SlideShare a Scribd company logo
1 of 13
Download to read offline
Operators
Precedence and Associativity of Operators
Operators
( )

[ ]

++(prefix)
+(unary)

.

->

Associativity
--(postfix)

left to right

--(prefix)
!
~
sizeof(type)
-(unary)
&(address)
*(dereference)

right to left

*

++(postfix)

/

%
-

<<
<

<=

left to right

>>

+

left to right

left to right

>

==

>=

left to right

!=

left to right

&
^

/=

left to right

? :
*=

left to right

||
-=

left to right

&&

+=

left to right

|

=

left to right

right to left

%=

>>=

<<=

&=

^=

|=

right to left
2
Operators
 Comparison:
C연산자

Description

조건의 예

==

=

a == 2

!=

같지않다

a != 3

<

<

a<3

>

>

a>4

<=



a <= 5

>=



a >= 3

!

부정

!(a < 3)

&&

and

(3 < a) && (a < 5)

||

or

(a < 3) || ( 5 < a)

3
Operators
 Arithmetic
Operator

Description

Example

+

+

a+2

-

-

a–3

*

* (곱하기)

a*3

/

/ (나누기 혹은 몫)

a/4

%

나머지

a%5

++

Incremental

a++, ++a

--

Decremental

a--, --a

4
Operators
 Assignment
Operator

Description

Example

=

assignment

a=b

+=

a=a+3

a += 3

-=

a=a-3

a -= 3

*=

a=a*3

a *= 3

/=

a=a/3

a /= 3

%=

a=a%3

a %= 3

5
Increment and Decrement Operators
 Increment operator
Postfix k++
Prefix ++k

 k = k - 1 ; 와 동일

 Decrement operator
Postfix k-Prefix --k

 k = k - 1 ; 와 동일

그러나 다른 연산자와 함께 사용되면 차이가 있음.
Prefix : operand가 사용되기 전에 먼저 증가/감소 시킴
Postfix : operand가 사용된 후에 증가/감소 시킴

6
Increment and Decrement Operators
[Ex]

int i = 4, j;
j = ++i + 3;
printf(“i : %d, j = %dn”, i, j);
j = i++ + 3;
printf(“i : %d, j = %dn”, i, j);
j = --i + 3;
printf(“i : %d, j = %dn”, i, j);
j = i-- + 3;
printf(“i : %d, j = %dn”, i, j);

i를 먼저 증가시킨 후,
j는 그 값에 3을 더함
j는 i에 3을 더한 값이 되고
그 후에 i를 1증가시킴

i = 5,
i = 6,
i = 5,
i = 4,

j
j
j
j

=8
=8
=8
=8
7
Bit Operators
 <<, >>, |, &, ^,~
– << : 왼쪽으로 주어진 수 비트만큼 shift
a = 100 << 1 ;
printf( “%d”, a )

100 -> 01100100 이므로
왼쪽으로 한비트 shift하면
11001000 (=200)

– >> : 오른쪽으로 주어진 수 비트만큼 shift
a = 100 >> 1 ;
printf( “%d”, a )

100 -> 01100100 이므로
오른쪽으로 한비트 shift하면
001100100 (=50)

8
Bit Operators
 <<, >>, |, &, ^,~
– | : 비트끼리 OR 연산
a = 100 | 200 ;
printf( “%d”, a )
– & : 비트끼리 AND 연산
a = 100 & 200 ;
printf( “%d”, a )
– ^:비트끼리 XOR
a = 100 ^ 200 ;
printf( “%d”, a )

(8bit 컴퓨터)
100 -> 01100100
200 -> 11001000 이므로
------------------a
= 11101100 (236)
100 -> 01100100
200 -> 11001000 이므로
------------------a
= 01000000 (64)

100 -> 01100100
200 -> 11001000 이므로
------------------a
= 10101100 (172)
9
Bit Operators
 <<, >>, |, &, ^,~
– ~ : 비트반전
a = ~100;
printf( “%d”, a )

(8bit 컴퓨터)
100 -> 01100100 이므로
------------------a
= 10011011 (155)

10
Example
[Ex]
i = 3 , j = 2 , k = 1;
if ( i > j > k ) printf( “Yes” );
else printf( “No” );
[Ex]
int a, i =1, j = 2, k;
a = i < 2 + j;
j = 5 * (k = 3);
printf( “%d %d %dn”, a, i, j, k );

 수학적인 표현의 의미로, j 가 i 와 k의 중간에 있다는
( i < j ) && ( j < k )
11
Example
 Example
int a = 5, b = 6, c = 7;
int d;
printf( “%d %d %dn, a < b, a > b, a != b );
d = (a < b) && (b < c );
printf( “%d %d %dn”, d, (a > b) || (b > c), !(!5) );

12
Assignment Operators
 Compound Assignment
– Compound Assignment operators
*=, -=, /=, %=, +=
op= [Ex]
– variable = variable op (expression)
=> variable op= expression

[Ex]

int k = 5;
k += 2;
k -= 2;
k *= 2;
k /= 2;
k %= 2;

/*
/*
/*
/*
/*

k = k + 2,
k = k - 2,
k = k * 2,
k = k / 2,
k = k % 2,

Left와 right side에 동일
variable을 사용하는 경우
compound assignment 로
simplify 가능

k=7
k=5
k=10
k=5
k=1

*/
*/
*/
*/
*/
13

More Related Content

More from 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
웅식 전
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
웅식 전
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
웅식 전
 

More from 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
 
W14 chap13
W14 chap13W14 chap13
W14 chap13
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
 

2 2. operators

  • 2. Precedence and Associativity of Operators Operators ( ) [ ] ++(prefix) +(unary) . -> Associativity --(postfix) left to right --(prefix) ! ~ sizeof(type) -(unary) &(address) *(dereference) right to left * ++(postfix) / % - << < <= left to right >> + left to right left to right > == >= left to right != left to right & ^ /= left to right ? : *= left to right || -= left to right && += left to right | = left to right right to left %= >>= <<= &= ^= |= right to left 2
  • 3. Operators  Comparison: C연산자 Description 조건의 예 == = a == 2 != 같지않다 a != 3 < < a<3 > > a>4 <=  a <= 5 >=  a >= 3 ! 부정 !(a < 3) && and (3 < a) && (a < 5) || or (a < 3) || ( 5 < a) 3
  • 4. Operators  Arithmetic Operator Description Example + + a+2 - - a–3 * * (곱하기) a*3 / / (나누기 혹은 몫) a/4 % 나머지 a%5 ++ Incremental a++, ++a -- Decremental a--, --a 4
  • 5. Operators  Assignment Operator Description Example = assignment a=b += a=a+3 a += 3 -= a=a-3 a -= 3 *= a=a*3 a *= 3 /= a=a/3 a /= 3 %= a=a%3 a %= 3 5
  • 6. Increment and Decrement Operators  Increment operator Postfix k++ Prefix ++k  k = k - 1 ; 와 동일  Decrement operator Postfix k-Prefix --k  k = k - 1 ; 와 동일 그러나 다른 연산자와 함께 사용되면 차이가 있음. Prefix : operand가 사용되기 전에 먼저 증가/감소 시킴 Postfix : operand가 사용된 후에 증가/감소 시킴 6
  • 7. Increment and Decrement Operators [Ex] int i = 4, j; j = ++i + 3; printf(“i : %d, j = %dn”, i, j); j = i++ + 3; printf(“i : %d, j = %dn”, i, j); j = --i + 3; printf(“i : %d, j = %dn”, i, j); j = i-- + 3; printf(“i : %d, j = %dn”, i, j); i를 먼저 증가시킨 후, j는 그 값에 3을 더함 j는 i에 3을 더한 값이 되고 그 후에 i를 1증가시킴 i = 5, i = 6, i = 5, i = 4, j j j j =8 =8 =8 =8 7
  • 8. Bit Operators  <<, >>, |, &, ^,~ – << : 왼쪽으로 주어진 수 비트만큼 shift a = 100 << 1 ; printf( “%d”, a ) 100 -> 01100100 이므로 왼쪽으로 한비트 shift하면 11001000 (=200) – >> : 오른쪽으로 주어진 수 비트만큼 shift a = 100 >> 1 ; printf( “%d”, a ) 100 -> 01100100 이므로 오른쪽으로 한비트 shift하면 001100100 (=50) 8
  • 9. Bit Operators  <<, >>, |, &, ^,~ – | : 비트끼리 OR 연산 a = 100 | 200 ; printf( “%d”, a ) – & : 비트끼리 AND 연산 a = 100 & 200 ; printf( “%d”, a ) – ^:비트끼리 XOR a = 100 ^ 200 ; printf( “%d”, a ) (8bit 컴퓨터) 100 -> 01100100 200 -> 11001000 이므로 ------------------a = 11101100 (236) 100 -> 01100100 200 -> 11001000 이므로 ------------------a = 01000000 (64) 100 -> 01100100 200 -> 11001000 이므로 ------------------a = 10101100 (172) 9
  • 10. Bit Operators  <<, >>, |, &, ^,~ – ~ : 비트반전 a = ~100; printf( “%d”, a ) (8bit 컴퓨터) 100 -> 01100100 이므로 ------------------a = 10011011 (155) 10
  • 11. Example [Ex] i = 3 , j = 2 , k = 1; if ( i > j > k ) printf( “Yes” ); else printf( “No” ); [Ex] int a, i =1, j = 2, k; a = i < 2 + j; j = 5 * (k = 3); printf( “%d %d %dn”, a, i, j, k );  수학적인 표현의 의미로, j 가 i 와 k의 중간에 있다는 ( i < j ) && ( j < k ) 11
  • 12. Example  Example int a = 5, b = 6, c = 7; int d; printf( “%d %d %dn, a < b, a > b, a != b ); d = (a < b) && (b < c ); printf( “%d %d %dn”, d, (a > b) || (b > c), !(!5) ); 12
  • 13. Assignment Operators  Compound Assignment – Compound Assignment operators *=, -=, /=, %=, += op= [Ex] – variable = variable op (expression) => variable op= expression [Ex] int k = 5; k += 2; k -= 2; k *= 2; k /= 2; k %= 2; /* /* /* /* /* k = k + 2, k = k - 2, k = k * 2, k = k / 2, k = k % 2, Left와 right side에 동일 variable을 사용하는 경우 compound assignment 로 simplify 가능 k=7 k=5 k=10 k=5 k=1 */ */ */ */ */ 13