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]