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]