1. 대입연산자
(1) 종류 : 왼 = 오 (오른쪽에서 왼쪽으로 대입)
(2) 예제
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int a = 3, b = 5, temp=0; // 변수 선언
printf("교환전 : a= %d, b= %d\n", a, b);
temp=b; //b를 temp에 대입
b=a; // a를 b에 대입
a=temp; // temp를 a에 대입
printf("교환후 : a= %d, b= %d\n",a ,b);
return 0;
}
2. 산술연산자
(1) 종류 : + 더하기 - 빼기 * 곱하기 / 나누기 % 나머지 구하기(정수만 가능)
(2) 예제1(소스코드, 실행결과, 주석)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int a, b;
printf("두개의 숫자를 입력하시오\n");
scanf("%d%d", &a, &b);
printf("%d + %d =%d\n", a, b, a + b);// 더하기 연산
printf("%d - %d =%d\n", a, b, a - b);// 빼기 연산
printf("%d * %d =%d\n", a, b, a*b);// 곱하기 연산
printf("%d / %d =%d\n", a, b, a / b);// 나누기 연산
printf("%d %% %d = %d\n\n", a, b, a%b);// 나머지 구하기
return 0;
}
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
char ch = 'A'; // A라는 문자의 아스키 코드값을 저장
int x = 10, y = 4, sum = 0;
double pi = 3.14, avg;
sum = ch + 2; //자동형 넓힘 변환. char형(1byte) 에서 int형(4byte)으로 들어가면서 자동으로 모양이 넓혀짐.
printf("sum = %d = %c \n", sum, sum);
sum = pi; // 자동형 좁힘 변환. double형(8byte) 에서 int형(4byte)으로 컴파일러가 자동으로 모양을 좁혀서 넣음.
printf("sum = %d \n", sum);
printf("x/y = %d", x/y); // 정수/정수 = 정수
// 강제형변환 : 실수/실수 = 실수
printf("x/y = %lf", (double)x/(double)y); //강제적으로 자료형을 변환시킬수있음
sum = (double)x/(double)y; // 강제형변환 후 자동 형좁힘 ( 실수형 -> 정수형 )
printf("잘못된 나눗셈 = %d\n", sum);
avg = (double)(x/y); // 2.5가 아닌 2.0으로 나온 이유 x,y가 이미 정수형으로 연산된 후의 값에 강제형변환을 해서 정확한 값이 안나옴.
printf("avg = %lf\n", avg);
avg = (double)x/y;
printf("정확한 나눗셈 = %lf\n", avg);
return 0;
}
3. 복합대입연산자
(1) 종류 : +=, -+, *=, /=, %/, >>=, <<=, &=, |=, ^=
(2) 예제 (소스코드, 실행결과, 주석)
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
// 복합대입연산자
int x,y;
x = y = 5;
printf("x = %d, y = %d\n", x, y);
printf("x += y의 결과는 %d\n", x+=y); // x = x + y
printf("x -= y의 결과는 %d\n", x-=y); // x = x - y
printf("x *= y의 결과는 %d\n", x*=y); // x = x * y
printf("x /= y의 결과는 %d\n", x/=y); // x = x / y
printf("x %%= y의 결과는 %d\n", x%=y); // x = x % y
return 0;
}
4. 관계연산자
(1) 종류 : >, <, >=, <=, ==, !=
(2) 예제 (소스코드, 실행결과, 주석)
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
// 관계연산자
int x,y;
printf("두정수 입력 : ");
scanf("%d%d", &x, &y);
printf("%d > %d 의 결과는 %d입니다.\n", x, y, x>y); // 결과가 참인 경우 리턴값이 1
printf("%d == %d 의 결과는 %d입니다.\n", x, y, x==y);
// 결과가 거짓인 경우 리턴값이0
return 0;
}
5. 논리연산자
(1) 종류 : &&, || !
(2) 예제 (소스코드, 실행결과, 주석)
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int x,y,z;
x = 5; y = 10; z;
z = (x+=2) || (y+=1);
printf("x=%d,y=%d\n", x, y);
printf("z=%d\n", z); // z = (x+=2) || (y+=1) 에서 x = 7이 되고 y = 11이 됨 0이 아닌 수는 참이므로 or 연산에서 참이 하나라도 있으면 결과값이 1
return 0;
}
6. 조건연산자
(1) 종류 : ?:
(2) 예제 (소스코드, 실행결과, 주석)
/*조건연산자 (조건식? 참: 거짓)
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n1,n2,max,min;
printf("두정수 입력 : ");
scanf("%d%d", &n1, &n2);
(n1>n2) ? (max=n1 , min=n2) : (max=n2 , min=n1);
printf("\n>> 큰 수 / 작은 수 = %d\n", max / min);
printf(">> 큰 수 %% 작은 수 = %d\n", max % min);
return 0;
}
7. 증감연산자
(1) 종류 : ++, --
(2) 예제 (소스코드, 실행결과, 주석)
//증감연산자 ++, --
// ++x (진위형, prefix) : x값 1증가후 연산수행
// --x
// x++ (후위형, postfix) : x값 연산후 1증가
// x--
//int x=5;
/*
int y=x++; // x는 y에 대입연산후 1증가
printf("x=%d\n", x);
printf("y=%d\n", y);
*/
/*
int y=++x; // x가 1증가후 y에 대입연산됨
printf("x=%d\n", x);
printf("y=%d\n", y);
*/
int a = 10, b = 20;
printf("a=%d\n", a++); // a에 10의 값이 대입연산후 1증가
printf("a=%d\n", ++a); // 1의 값이 증가한후 a에 대입연산
printf("a=%d\n", b--); // b에 20의 값이 대입연산후 1감소
printf("a=%d\n", --b); // 1의 값이 감소한후 b에 대입연산
return 0;
}
8. 비트연산자
(1) 종류 : &, |, ^, ~, <<, >>
(2) 예제 (소스코드, 실행결과, 주석)
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int x;
printf("정수입력 : ");
scanf("%d", &x);
printf("%d>>3=%d\n", x, x>>3); // 비트를 3번 이동한뒤 나누는 연산 (피연산자 나누기 2^3이 결과값으로 나옴)
printf("%d<<3=%d\n", x, x<<3); // 비트를 3번 이동한뒤 곱하는 연산 (피연산자 곱하기 2^3이 결과값으로 나옴)
return 0;
}
9. 연산자의 우선순위
연산자 분류에 따른 우선순위 : 단항 > 산술 > 이동 > 관계 > 비트 > 논리 > 조건 > 대입 > 콤마 순이다.
연산자의 우선순위가 기억이 나지않는다면 괄호를 사용하여 강제로 우선순위를 지정할수있다.
'basic > C' 카테고리의 다른 글
6. 함수 (0) | 2016.10.31 |
---|---|
5. 배열 (0) | 2016.10.13 |
4. 제어문(조건문과 반복문) (0) | 2016.10.06 |
2. C프로그래밍의 기초문법 (0) | 2016.09.08 |
1. C프로그래밍의 기초 (0) | 2016.09.05 |