728x90
CH03 연산자
1. 연산자와 우선순위
연산자(operator)
- 자료에 대한 연산동작을 지정한 기호
구분 | 연산자 |
산술 연산자 | +, -, /, *, %, ++, -- |
관계 연산자 | >, <, >=, ==, != |
논리 연산자 | &&, ||, ! |
대입 연산자 | +=, -=, *=, /=, %=, <<=, >>=, |=, &= |
조건 연산자 | ?, : |
비트 연산자 | &, |, ^, ~, <<, >> |
기타 연산자 | sizeof(), cast, &, * |
연산자 우선순위와 결합방향
- 연산대상, 피연산자의 개수로 구별
- 단항 연산자, 이항연산자, 삼항 연산자
우선순위 | 종류 | 연산자 | 결합방향 | |
단항 연산자 | 1 | 1차 연산자 | (), [], ->, . | → |
2 | 단항ㅅ 연산자 | !, ~, ++, --, &, *, sizeof(), cast | ← | |
이항 연산자 | 3 | 산술 연산자 |
/, *, % | → |
4 | +, - | |||
5 | 비트 이동 연산자 | <<, >> | ||
6 | 관계 연산자 |
>, <, >=, <= | ||
7 | ==, != | |||
8 | 비트 논리 연산자 | & | ||
9 | ^ | |||
10 | | | |||
11 | 논리 연산자 |
&& | ||
12 | || | |||
삼항 연산자 | 13 | 조건 연산자 | ?, : | ← |
14 | 대입 연산자 | +=, -=, *=, /=, %=, <<=, >>=, |=, &= |
← | |
15 | 나열 연산자 | , (콤마) | → |
2. 산술 연산자
이항 연산자
# 예시
#include <stdio.h>
void main()
{
int x, y;
x = 5;
y = 2;
printf("덧셈: 5+2 = %d", x+y); # 결과 7
printf("뺄셈: 5-2 = %d", x-y); # 결과 3
printf("곱셈: 5*2 = %d", x*y); # 결과 10
printf("나눗셈: 5/2 = %d", x/y); # 결과 2
printf("나머지: 5%2 = %d", x%y); # 결과 1
}
단항 연산자
전위(먼저) | 후위(나중에) |
++x | x++ |
--x | x-- |
# 예시
#include <stdio.h>
void main()
{
int x, y;
x = 10;
y = ++x;
printf("x : %d", x); # 결과 11
printf("y : %d", y); # 결과 11
x = 10;
y = x++;
printf("x : %d", x); # 결과 11
printf("y : %d", y); # 결과 10
}
3. 관계 연산자
- 관계에 대한 결과를 논리값으로 출력
- 참(1) or 거짓 (0)
# 예시
#include <stdio.h>
void main()
{
int x, y;
x = 5;
y = 2;
printf("5 > 2: %d", x > y); # 결과 1
printf("5 < 2: %d", x < y); # 결과 0
printf("5 == 2: %d", x == y); # 결과 0
printf("5 != 2: %d", x != y); # 결과 1
}
4. 논리 연산자
- NOT(논리부정): !
- AND(논리곱): &&
- OR(논리합): ||
# 예시
#include <stdio.h>
void main()
{
int month, day, birthady, jan;
month = 1;
day = 10;
birthday = month == 8 && day == 19;
printf("birthday : %d", birthday); # 결과 0
jan = month == 1 || day == 1;
printf("jan : %d", jan); # 결과 1
}
5. 대입 연산자
# 복합연산자 예시
#include <stdio.h>
void main()
{
int a = 10;
a = a + 1;
printf("%d", a); # 결과 11
a += 1;
printf("%d", a); # 결과 12
++a;
printf("%d", a); # 결과 13
a++;
printf("%d", a); # 결과 14
}
6. 조건 연산자
- 항1(조건식) ? 항2 : 항3
- 조건식이 참이면 항2 거짓이면 항3
#삼항연산자 예시
#include <stdio.h>
void main()
{
int x, y;
x = 5;
y = 2;
max = (x > y) ? x : y;
printf("큰값 : %d", max); # 결과 5
}
7.비트 연산자와 기타 연산자
구분 | 연산자 |
비트 연산자 | &, |, ^, ~, <<, >> |
기타 연산자 | sizeof(), cast, &, * |
#include <stdio.h>
void main()
{
printf("int형의 크기: %d바이트", sizeof(int)); # 결과 4
printf("float형의 크기: %d바이트", sizeof(float)); # 결과 4
printf("double형의 크기: %d바이트", sizeof(double)); # 결과 8
printf("char형의 크기: %d바이트", sizeof(char)); # 결과 1
}
- sizeof() 함수는 피연산자의 크기를 바이트 단위로 반환한다
# 형변환연산자(cast) 예시
#include <stdio.h>
void main()
{
int x, y;
x = 5;
y = 2;
printf("x / y: %d", x / y); # 결과 2
printf("x / y %d", (double)x / (double)y); # 결과 2.5
- double은 형변환 연산자로, 변수를 실수형으로 변환한다.
728x90
'자격증' 카테고리의 다른 글
[정보처리기사] 유튜브 두목넷 정보처리기사 실기를 위한 C 특강(6강 배열) (0) | 2024.02.17 |
---|---|
[정보처리기사] 유튜브 두목넷 정보처리기사 실기를 위한 C 특강(5강 제어문-반복구조) (0) | 2024.02.15 |
[정보처리기사] 유튜브 두목넷 정보처리기사 실기를 위한 C 특강(4강 제어문-선택구조) (0) | 2024.02.14 |
[정보처리기사] 유튜브 두목넷 정보처리기사 실기를 위한 C 특강(2강 변수와 상수) (1) | 2024.02.08 |
[정보처리기사] 유튜브 두목넷 정보처리기사 실기를 위한 C 특강(1강 기본적 문법 구조) (1) | 2024.02.07 |