This chapter takes a long look at C's rich set of operators. We examine more closely the operators we've already introduced, and we introduce the operators we previously ignored. We pay special attention to the operators that have no analog in most other programming languages, such as the shorthand assignment and bit-manipulation operators. Many of C's operators are just similar enough to those of other programming languages to cause problems, so we spend much of our time on their caveats and quirks. The chapter concludes with a case study: a pair of programs that compress and uncompress their input.
Jump to: [Previous Chapter | Next Chapter]
int i, j = 5; i = j++; i = ++j; i값은 5 6 j값은 6 6
expr1 ? expr2 : expr3 // expr1이 TRUE면 expr2, FALSE면 expr3 수행
diff = (x > y) ? (x - y) : (y - x);
~ : Complement
& : AND
| : OR
^ : XOR
<< : Shift left
>> : Shift right
=, op= (+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=)
, (Comma)
sizeof
(., ->, &, *) : pointer 관련 연산자
우선 순위가 같을 경우 associativity에 따라 수행
( ) [ ] . -> |
--> |
Unary operators: ! ~ ++ -- - sizeof * & (type) |
<-- |
* / % |
--> |
+ - |
--> |
<< >> |
--> |
< <= > >= |
--> |
== != |
--> |
& |
--> |
^ |
--> |
| |
--> |
&& |
--> |
|| |
--> |
? : |
<-- |
= += -= *= /= %= &= ^= |= <<= >>= |
<-- |
| , | --> |
a = a + b; a += b;
a = a - b; a -= b;
a = a % b; a %= b;
a = a + 1; a++;
a = a - 1; a--;
printf("prompt string");
variable = GetValue();
for (i = 0; i < N; i++) { // N번 반복. i는 index variable이라 함
반복 수행할 문들
}
while (조건식) {
반복 수행할 문들
}
do {
반복 수행할 문들
} while (조건식)
if (조건) {
조건이 참일 경우 수행할 문
}
if (조건) {
조건이 TRUE일 경우 수행할 문
} else {
조건이 FALSE일 경우 수행할 문
}
예)
int a, b, c; b = 1; c = -1; // b < c, b != c, b && c, b >= 0 && c <= 0 a = b || c;
예제) x가 윤년일 경우 TRUE인 조건식을 작성하라.
(leapyear.c)
(x가 400의 배수이거나 4의 배수이면서 100의 배수가 아니면 TRUE)