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)