CHAPTER 6
OPERATORS

[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]
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]


  1. OPERATORS, OPERANDS, AND PRECEDENCE
  2. THE RELATIONAL OPERATORS
  3. THE LOGICAL OPERATORS
  4. BITWISE OPERATORS
  5. ASSIGNMENT OPERATORS
  6. OTHER OPERATORS
  7. CASE STUDY: DATA COMPRESSION

¿¬»êÀÚ (Operators)

  1. »ê¼ú ¿¬»êÀÚ (Computational operators) : +, -, *, /, %, ++, --
    ¼öÄ¡ °è»ê
       int i, j = 5;
       i = j++;      i = ++j;
    
    i°ªÀº    5        6
    j°ªÀº    6        6
    

  2. °ü°è ¿¬»êÀÚ (Relational operators) : <, <=, >, >=, ==, !=
    ¼öÄ¡ ºñ±³

  3. ³í¸® ¿¬»êÀÚ (Logical operators) : &&, ||, ! [Boolean operation]
    ³í¸®°ª¿¡ ´ëÇÑ ³í¸® ¿¬»ê (AND, OR, NOT)

  4. Á¶°Ç ¿¬»êÀÚ (Conditional operator or ternary operator) : ? :
              expr1 ? expr2 : expr3 // expr1ÀÌ TRUE¸é expr2, FALSE¸é expr3 ¼öÇà
    diff = (x > y) ? (x - y) : (y - x);

  5. ºñÆ® ¿¬»êÀÚ (Bitwise operators) :
              ~ : Complement
    & : AND
    | : OR
    ^ : XOR
    << : Shift left
    >> : Shift right

  6. ´ëÀÔ ¿¬»êÀÚ (Assignment operator)
              =, op= (+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=)

  7. ±âŸ ¿¬»êÀÚ (Miscellaneous operators)
          , (Comma)
          sizeof
          (., ->, &, *) : pointer °ü·Ã ¿¬»êÀÚ

¿¬»ê ¿ì¼± ¼øÀ§

¿ì¼± ¼øÀ§°¡ °°À» °æ¿ì associativity¿¡ µû¶ó ¼öÇà

Operators
Associativity
( )   [ ]   .   ->
-->
Unary operators: !  ~  ++  --  -  sizeof  *  &  (type)
<--
*   /   %
-->
+   -
-->
<<   >>
-->
<   <=   >   >=
-->
==    !=
-->
&
-->
^
-->
|
-->
&&
-->
||
-->
? :
<--
=  +=  -=  *=  /=  %=  &=  ^=  |=  <<=  >>=
<--
, -->

¼­·Î ´Ù¸¥ ÀÚ·áÇü¿¡ ´ëÇÑ ¿¬»ê ½Ã C´Â ÀÚµ¿ Çü º¯È¯ ±â´ÉÀ» Á¦°øÇÑ´Ù. (Implicit type castingÀ̶ó ÇÔ) ±×·¯³ª Type casting operationÀ» ÀÌ¿ëÇÑ explicit type casting [Type_name (expression)]ÀÌ ¹Ù¶÷Á÷ÇÏ´Ù.



C ÇÁ·Î±×·¥¿¡¼­ ÀÚÁÖ »ç¿ëµÇ´Â °ü¿ëÀûÀΠǥÇö

             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ÀÏ °æ¿ì ¼öÇàÇÒ ¹®
          }


* C ÇÁ·Î±×·¥¿¡¼­ 0Àº FALSE, 0 ÀÌ¿ÜÀÇ ´Ù¸¥ ¸ðµç °ªµéÀº TRUE·Î Ãë±ÞµÈ´Ù.

¿¹)

   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)


[ Table Of Contents | Previous Chapter | Next Chapter]