5차시:
어휘 원소, 연산자, C 시스템 (계속)
• 프로그래밍 및 실험
• 제 5주
• 동국대학교 조영석
이 문서는 나눔글꼴로 작성되었습니다. 설치하기
2
2.8 연산자와 구두점
2.9 연산자의 우선순위와 결합법칙 - 연산자 : + - * / %(modulus)
- 연산자 : 컴파일러에게 연산의 종류를 알림 - 구두점 : , { } ( ) ;
- 구두점 : 컴파일러에게 구분을 알림 - main() 의 ()는 연산자로 취급
- 연산자 우선순위와 결합법칙
연산자 결합법칙
( ) ++(후위) --(후위) 좌에서 우로
+(단항) -(단항) ++(전위) --(전위) 우에서 좌로
* / % 좌에서 우로
+ - 좌에서 우로
= += -= *= /= etc. 우에서 좌로
3
(예)
- a * b – c → (((- a) * b) - c) a +++ b → ((a ++) + b)
a +=++ b → (a += (++ b))
2.10 증가 연산자와 감소 연산자 - ++i, --i, i++, i--
- a = ++i;
(i = i + 1; a = i;)와 같은 의미, --i의 경우도 마찬가지 - a = i++;
(a = i; i = i + 1;)과 같은 의미, i--의 경우도 마찬가지 - int a, b, c = 0;
- a = ++c; /* a = (c = c + 1); a는 1, c는 1 */
- b = c++; /* b는 1, c는 2 */
- printf("%d %d %d \n", a, b, ++c); /* c는 3, 출력 */
- (실행결과) - 1 1 3
5
- int i = 0;
printf("%d", i++); /* 0 이 출력 됨, i는 출력후 1이 됨 */
printf("%d", i); /* 1 이 출력 됨 */
printf("%d", ++i); /* 2 가 출력 됨 */
printf("%d", i); /* 2 가 출력 됨 */
- 선언 및 초기화
int a= 1, b = 2, c = 3, d = 4
수식 동일한 수식 결과값
a * b /c (a * b) / c 0
a * b % c + 1 ((a * b) % c) + 1 3 ++a *b - c -- ((++ a) * b) - (c --) 1 7 - - b * ++ d 7 - ((-b) * (++ d)) 17
2.11 배정 연산자: =
- (예) b = 2;
a = b + c;
a = (b = 2) + (c = 3);
a = b = c = 0; /* a = (b = (c = 0)); */
k = k + 2;
- 배정 연산자의 종류
= a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
- (주의) a *= b + c; a = a * (b + c);
NOT a = (a * b) + c;
7
>>= (bitwise) shift right equal operator (예) i = 10;
i >>= 2; /* i = i >> 2 */
(1010)2 = 10 (0010)2 = 2
<<= (bitwise) shift left equal operator (예) i = 10;
i <<= 2; /* i = i >> 2 */
(1010)2 = 10 (101000)2 = 40 shift operation: 10진수의 예
shift right (1자리)
→ shift left (1자리)
→
0 0 0 0 1 0 1 0
0 0 0 0 0 0 1 0 1 0
0 0 0 0 1 0 1 0
0 0 0 0 1 0 1 0 0 0
0 2 5 6 0 0 2 5 6
0 2 5 6 0 2 5 6 0
>>= (bitwise) shift right equal operator
- 1 bit를 shift right할 때마다 ½ 값이 되며 underflow가 일 어남.
-(예) x = 11; /* (11)10 = (1011)2 */
x = x >> 1; /* (1011)2 >> 1 == (0101)2 == 5 */
x = x >> 1; /* (0101)2 >> 1 == (0010)2 == 2 */
x = x >> 1; /* (0010)2 >> 1 == (0001)2 == 1 */
x = x >> 1; /* (0001)2 >> 1 == (0000)2 == 0 */
(주의) 컴퓨터에 따라 앞에서 (1)2이 채워지는 경우가 있음.
<<= (bitwise) shift left equal operator
- 1 bit를 shift left할 때마다 2배의 값이 되며 overflow가 일어남.
- (예) x = 1; /* (1)10 = (0001)2 */
x = x << 1; /* (0001)2 << 1 == (0010)2 == 2 */
x = x << 1; /* (0010)2 << 1 == (0100)2 == 4 */
x = x << 1; /* (0100)2 << 1 == (1000)2 == 8 */
x = x << 1; /* (1000)2 << 1 == (0000)2 == 0 */
9
&= bitwise AND /* ampersand */
(예) | 1010 (예 |& 0110 (예) | 0010
^= bitwise EXCLUSIVE OR /*’^’: carrot, 또는 carat */
(예) | 1010 (예) ^ 0011 (예) | 1001
|= bitwise OR /* vertical bar */
(예) | 1010 (예) | 0110 (예) | 1110
선언 및 초기화
int i = 1, j = 2, k = 3, m = 4
수식 동일한 수식 동일한 수식 결과값
i += j + k
j *= k = m + 5
i += (j + k)
j *= (k = (m + 5))
i = (i + (j + k))
j = (j * (k = (m + 5)))
6 18
11
2.12 예제 : 2의 거듭제곱 계산
/* Some powers of 2 are printed. */
#incluse <stdio.h>
int main (void) {
int i = 0, power = 1;
while (++i <= 10)
printf("%-6d", power *= 2);
printf("\n");
return 0;
}
%-6d
%6d
2
2
2의 거듭제곱 계산-권장 PGMG
#incluse <stdio.h>
int main (void) {
int i = 0, power = 1;
while (++i <= 10)
printf("%-6d", power *= 2);
printf("\n");
return 0;
}
#incluse <stdio.h>
int main (void) {
int i = 1, power = 1;
while (i <= 10) {
power = power * 2;
i = i + 1;
} printf("%-6d\n", power);
return 0;
}
13
2.13 C 시스템
- 전처리기: 전처리기 지시자 수행
- 표준라이브러리
• 유용한 함수들을 포함
• 함수들의 원형을 포함 - 적절한 header file을 #include
• 컴파일된 함수의 목적코드(object code) 포함 object code는 header file에는 포함되지 않음.
(예 1) 50 random numbers 출력 program
#include <stdio.h>
#include <stdlib.h>
void main() {
int i, n = 50;
for (i = 0; i < n; ++i) { if (i % 8 == 0)
putchar('\n');
printf("%7d", rand());
}
} /* return 문이 생략됨. void */
15
(예 2): 실행시킬 때 마다 다른 random number stream을 출력
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main() /* int, void 등이 생략됨 - int */
{
int i, n, seed;
....
seed = time(NULL);
srand(seed);
....
i = rand();
...
return 0;
}
- 간략한 표현
• seed = time(NULL);
srand(seed);
• srand (time(NULL));
- ...
srand (time (NULL));
...
for (i = 1; i <= 50; ++i)
printf("%8d", rand());
...
이 문서는 나눔글꼴로 작성되었습니다. 설치하기