2 types, operators, and expressions(형 연산자및식e.smu.ac.kr/classes/2020-hael0001/slides/2...
TRANSCRIPT
2 Types, operators, and
expressions(형, 연산자 및 식)
Contents
2.0 Preliminary(사전 지식)
2.1 Variable names(변수 이름)
2.2 Data types and sizes(데이터 형과 크기)
2.3 Constants(상수)
2.4 Declarations(선언)
2.5 Arithmetic operators(산술 연산자)
2.6 Relational and logical operators(관계 및 논리 연산자)
2.7 Type conversions(형 변환)
2.8 Increment and decrement operators(증가 및 감소 연산자)
2.9 Bitwise operators(비트 연산자)
2.10 Assignment operators and expressions(지정 연산자와 식)
2.11 Conditional operator(조건 연산자)
2.12 Precedence and order of evaluation(우선 순위 및 계산 순서)
2 Types, operators, and expressions(형, 연산자 및 식) 2-2
Summary(요약)
• Variable(변수) 및 constant(상수)는 C 프로그램이 다루는 기본적인 data object임
• Declaration(선언)은 C 프로그램에서 사용되는variable의 type(형) 및 initial value(초기 값)를 지정하는 것임
• Operator(연산자)는 variable 및 constant 등을 가지고 구성할 수 있는 operation(계산)을 의미함
• 어떤 data object의 type은 그 data object이 가질수 있는 value(값) 및 operator를 지정함
• Expression(식)은 variable과 constant 및 operator를사용하여 어떤 계산을 표현한 것임
2 Types, operators, and expressions(형, 연산자 및 식) 2-3
2.0 Preliminary(사전 지식)
• Bits, bytes, integers(비트, 바이트, 정수)
• 1’s complement(1의 보수)
• 2’s complement(2의 보수)
• Integer representation(정수 표현)
• ASCII(American Standard Code for Information Interchange)(미국 정보 교환 표준 부호)
• Variables, symbolic constants, constants(변수, 심볼상수, 상수)
• Operators(연산자), operands(피연산자),associativity(계산 방향), precedence(우선순위)
• Expressions(식)
2 Types, operators, and expressions(형, 연산자 및 식) 2-4
Bits, bytes, integers(비트, 바이트, 정수)
• 1개 bit(=binary digit)는 0 혹은 1을 표현하는 숫자 1개
• 1개 byte는 8개의 bit을 의미함 (예: 8개의 bit인10101010은 1개 byte임)
• 컴퓨터 내에서는 모든 정보를 bit으로 구성되는 2진수(binary)로 표현함
• 어떤 integer(정수)를 2진수로 쓰면 너무 길어 지기 때문에 8진수(octal) 혹은 16진수(hexa-decimal)로 변환하여짧게 쓰기도 함 (예: 10진수 1,431,655,765)
– 2진수: 0101 0101 0101 0101 0101 0101 0101 0101
– 8진수: 12 525 252 525
– 16진수: 5555 5555
2 Types, operators, and expressions(형, 연산자 및 식) 2-5
2진수의 8진수, 16진수 변환
• N개의 bit으로 표현되는 2진수 숫자를 8진수 숫자로 변환할 때는 2진수 숫자의 lsb(least significant bit: 제일 오른쪽 비트인 bit[0])에서msb(most significant bit: 제일 왼쪽 비트인bit[n-1])로 가면서 3개 bit씩 변환함
– 이진수 01010101 은 8진수로 125 임
• 2진수의 숫자를 16진수의 숫자로 변환할 때는 2진수 숫자의 lsb에서 msb로 가면서 4개 bit씩 변환함
– 이진수 01010101 은 16진수로 55 임
2 Types, operators, and expressions(형, 연산자 및 식) 2-6
Windows의 계산기(프로그래머용)
2 Types, operators, and expressions(형, 연산자 및 식) 2-7
1’s complement(1의 보수)
• 어떤 2진수의 “1의 보수”는 그 2진수를 구
성하는 bit이 0 이면 1로, 1 이면 0 으로 바
꾸어(complement) 표현한 2진수임
• 아래는 1 byte로 표현된 어떤 2진수의 “1
의 보수”를 표현한 2진수임
▪ 01010101 의 1의 보수는 10101010 임
▪ 00000001 의 1의 보수는 11111110 임
2 Types, operators, and expressions(형, 연산자 및 식) 2-8
2’s complement(2의 보수)
• 어떤 2진수의 “2의 보수”는 (그 2진수의 “1
의 보수” + 1)로 표현되는 2진수임
• 아래는 1 byte로 표현된 어떤 2진수의 “2
의 보수”를 표현한 2진수임
▪ 01010101 의 2의 보수는 (10101010 +
1) 즉 10101011 임
▪ 00000001 의 2의 보수는 (11111110 +
1) 즉 11111111 임
2 Types, operators, and expressions(형, 연산자 및 식) 2-9
Integer representation(정수 표현)
• 컴퓨터 내에서 정수(예: 여기서 보기로 8 bit 크기를 가정)의표현은 아래와 같음 (이렇게 표현하는 이유는 정수 연산 회로를 간단히 구현하기 위하여)▪ 양수 n는 msb 1 bit은 0, 나머지 7 bit에 양수 n을 표현▪ 0 은 00000000▪ 음수 –n는 양수 n의 2의 보수로 표현 (이 숫자의 msb는 항
상 1임)• 8 bit 크기의 정수를 가정하여 표현
▪ 2 는 00000010▪ 1 은 00000001▪ 0 은 00000000▪ -1는 11111111 (1의 2의 보수는 11111110+1임)▪ -2는 11111110 (2의 2의 보수는 11111101+1임)
참고: 8 bit로 표현되는 정수의 최소 값은 –27, 최대 값은+27-1임
2 Types, operators, and expressions(형, 연산자 및 식) 2-10
ASCII(American Standard Code for Information Interchange)
(미국 정보 교환 표준 부호)
• ASCII는 하나의 문자를 7 bit 숫자로 표현하는 미국 표준임 (예: 문자 A는 16진수로 41)
참고: 7 bit는 10진수(Decimal)로 0-127, 16진수(Hexa-decimal)로 0-7F, 8진수(Octal)로 0-177을 표현함2 Types, operators, and expressions(형, 연산자 및 식) 2-11
Variables, symbolic constants, constants(변수, 심볼 상수, 상수)
• Variable(변수)는 시간이 흐름에 따라 변화하는
어떤 값(value)을 저장하고 있음
• Symbolic constant(심볼 상수)는 고정된 어떤 값
을 가지고 있음
• 변수와 심볼 상수는 name(이름)을 가지고 있음
(예: 변수 sum, 심볼 상수 PI 등)
• Constant(상수)는 그 자체로 고정된 어떤 값을
표현함 (예: 숫자 1000, 문자 ‘A’ 등)
2 Types, operators, and expressions(형, 연산자 및 식) 2-12
Operators(연산자), operands(피연산자),associativity(계산 방향), precedence(우선순위)
• Operator(연산자)는 가장 간단한 어떤 계산을 표현함
– 연산자 +는 더하기 계산을 연산자 *는 곱하기 계산을표현함
• 각 연산자는 정해진 개수(1개, 2개 혹은 3개 등)의operand(피연산자)를 요구함
– 연산자 +는 2개의 피연산자를 필요로 함
• 하나의 연산자가 다수 개 사용될 때는 left-to-right 혹은right-to-left 중 하나의 associativity(계산 방향)이 있음
– 연산자 +의 계산 방향은 left-to-right임
• 모든 연산자는 상대적인 precedence(우선 순위)가 있음
– 연산자 *의 우선순위는 연산자 +의 우선순위보다 높다
2 Types, operators, and expressions(형, 연산자 및 식) 2-13
Expressions(식)
• Expression(식)은 변수, 심볼 상수, 상수, 연산자등을 사용하여 어떤 계산을 표현한 것
• 어떤 하나의 식을 계산하면 하나의 값이 도출됨(컴퓨터가 어떤 식을 계산하여 값을 도출하는 데는 시간이 걸림)
• 아래는 3개의 식의 계산() 결과를 보여줌 (여기서 변수 i의 현재 값은 10, 심볼 상수 PI의 값은3.14 라고 가정함)
▪ i + 20 30
▪ PI - 3 0.14
▪ 1 + 2 * 3 7
2 Types, operators, and expressions(형, 연산자 및 식) 2-14
2.1 Variable names(변수 이름)
• Variable(변수)/function(함수) 혹은 symbolic constant(심볼 상수)의 name(이름)으로 사용할 수 있는 것을 identifier(식별자)라고 함
– identifier는 letter(a-z, A-Z), underscore(_) 및 digit(0-9)을연결하여 구성함 (단 첫 문자는 digit이 될 수 없음)
– C 언어의 reserved word(예약어) 혹은 keyword는identifier가 될 수 없음 (예: if, for, while, return, int, ... 등)
참고:
① variable는 어떤 변화하는 값(value)를 저장하는 역할을 하고, function는 어떤 계산을 표현하는 역할을 하고, symbolic constant는 어떤 고정된 값을 표현함
② 모든 variable, function, symbolic constant는 name을 가지는데, identifier는 이들 이름이 될 수 있음
2 Types, operators, and expressions(형, 연산자 및 식) 2-15
Identifier(식별자) - 연습
• 다음 중 C 언어에서 identifier인 것을 찾아라
(1) 123, (2) abc123, (3) _abc_, (4) SIZE, (5) 123_abc, (6) if
• 다음 중 C 언어에서 reserved word 혹은keyword이어서 identifier가 될 수 없는 것을 찾아라
(1)home, (2) MAX, (3) for, (4) which, (5) k
참고: C 언어에서 reserved word는 수십개로 매우많은데 앞으로 나올 때 마다 하나씩 배우게 됨
2 Types, operators, and expressions(형, 연산자 및 식) 2-16
Variable names(변수 이름)
• 전통적으로 소문자(a-z)는 variable/function name에, 대문자(A-Z)는 symbolic constant name에 사용됨 (꼭 지켜야 되는 규칙은 아님)
• C 언어에서 identifier의 길이
– internal(함수 내부) 용: 적어도 첫 31개 문자까지는 중요함
– external(함수 외부) 용: internal 용의 경우보다 짧을 수 있음
2 Types, operators, and expressions(형, 연산자 및 식) 2-17
2.2 Data types and sizes(데이터 형과 크기)
• Data types(데이터 형)
• Data sizes(데이터 크기)
2 Types, operators, and expressions(형, 연산자 및 식) 2-18
Data types(데이터 형)
• Basic type 및 저장을 위한 메모리 크기 (메모리 크기는우리 oak 프로그래밍 서버에서의 크기이며 기계에 따라조금씩 다를 수 있음)
– char: 하나의 문자(ASCII로 표현), 8 bits (=1 byte)
– int: 하나의 정수(2'complement로 표현), 32 bits (=4 bytes)
– float: 하나의 single precision(기본 정밀도) floatingpoint(부동 소수점) 숫자(보통 IEEE 754 single precision으로 표현), 32 bits (=4 bytes)
– Double: 하나의 double precision(두배의 정밀도) 부동 소수점 숫자(보통 IEEE 754 double precision으로표현), 64 bits (=8 bytes)
2 Types, operators, and expressions(형, 연산자 및 식) 2-19
Data types(데이터 형)
• 앞 basic type에 다양한 qualifier(한정사)가 추가될 수 있음 (예: short, long, signed, unsigned 등)
▪ short int: 2 byte 크기의 int
▪ long int: 8 byte 크기의 int
▪ unsigned int: 음수 표현은 없는 4 byte 크기의int
• Operator(연산자) sizeof는 어떤 type 혹은expression의 값을 표현하기 위한 byte 수를 계산하는 연산자임
– 사용법: sizeof (형) 혹은 sizeof (식)
2 Types, operators, and expressions(형, 연산자 및 식) 2-20
Data sizes(데이터 크기)
• 어떤 컴퓨터의 C 언어에서 각 type이 표현하는 값의 최소 및 최대 범위는 아래 파일에 symbolic constant(심볼 상수)로 정의됨
– /usr/include/limits.h (char, int, long, short)
– /usr/lib/gcc/x86_64-linux-gnu/5.4.0/include/float.h (float, double, long double)
2 Types, operators, and expressions(형, 연산자 및 식) 2-21
/usr/include/limits.h
2 Types, operators, and expressions(형, 연산자 및 식) 2-22
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/include/float.h
2 Types, operators, and expressions(형, 연산자 및 식) 2-23
2.3 Constants(상수)
• Constant의 종류
▪ Char constant: 'x', '0', '\n’ (newline), ‘\0101' (octal 표현임), '\xff' (hexadecimal 표현임) 등
▪ Integer constant: 1234, -1L (long int 표현), 1U (unsignedint 표현), 013 (octal int 표현), 0xff (hexadecimal int 표현) 등
▪ Float constant: 12.45, 1.245e1 (지수 표현) 등
▪ String constant: "I am a string" 등
• 여러 개의 string constant를 나열하면 자동으로concatenate(연결)되어 하나의 string이 됨
예: "hello," " world"는 "hello, world"와 같음
주의: char constant 'x'와 string constant "x"는 다름
2 Types, operators, and expressions(형, 연산자 및 식) 2-24
2.4 Declarations(선언)
• Declaration이란 어떤 variable의 type 및 initial value를 지정하는 것
• Declaration의 기본 형식은 type variable_name = initial_value; (= initial_value는 생략 가능)
int lower;
int upper;
int step;
char c = 'x';
• 중요한 규칙: C 프로그램에서 모든 variable은 사용되는 위치의 앞 부분에 declare되어야 함
2 Types, operators, and expressions(형, 연산자 및 식) 2-25
간단히int lower, upper, step;
Declarations(선언) - 보기
• 아래와 같은 변수 선언은
① int lower, upper, step;
② char c = 'x';
• 다음과 같은 의미를 가짐
① 변수 lower, upper, step의 형은 모두 int임, 즉 각각 4 byte(총 12 byte)의 메모리가 필요함. (주: 어떤 값이 저장되어 있는지는 모름)
② 변수 c의 형은 char임, 즉 1 byte의 메모리가필요함, 변수 c의 초기 값은 ASCII 문자 ‘x’임(16진수로 78임)
2 Types, operators, and expressions(형, 연산자 및 식) 2-26
2.5 Arithmetic operators(산술 연산자)
• Arithmetic operator(5개)는 아래와 같이 구성됨
+ (add), - (subtract), * (multiply), / (divide), % (modulus)
• Operator %는 float 및 double에 적용될 수없음
• Arithmetic operator의 associativity(계산 방향)은 모두 left-to-right임
• Arithmetic operator의 precedence(우선 순위) 는 *, /, %는 높고 +, -는 낮음
2 Types, operators, and expressions(형, 연산자 및 식) 2-27
Arithmetic operators - 보기
• 산술 연산자를 사용한 계산 보기
▪ 1 + 2 + 3 6 // associativity 주의
▪ 1 + 2 * 3 7 // precedence 주의
▪ 9 % 5 % 3 1 // associativity 주의
▪ 1 + 10 % 3 2 // precedence 주의
2 Types, operators, and expressions(형, 연산자 및 식) 2-28
Arithmetic operators - 보기
• Leap year(윤년)인지 아닌지를 출력하는 프로그램
① year를 4로 나누어 나머지가 0이면서 100으로 나누어 나머지가 0이 아닌 경우
혹은
② year를 400으로 나누어 나머지가 0인 경우
if ((year % 4 == 0 && year % 100 != 0)
|| year % 400 == 0)
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
2 Types, operators, and expressions(형, 연산자 및 식) 2-29
2.6 Relational and logical operators(관계 및 논리 연산자)
• Relational operator(6개)는 아래와 같이 구성됨
> (greater than), >= (greater than or equal), < (less than), <= (less than or equal), == (equal), != (not equal)
• Logical operator(3개)는 아래와 같이 구성됨
&& (logical and), || (logical or), ! (logical not)
• Relational 및 logical operator의 계산 결과가 TRUE이면 1이고 FALSE이면 0임
• Logical operator의 계산에서 0이 아닌 수는 TRUE이고 0은FALSE임
• Relational operator 및 logical operator의 associativity(계산방향)은 모두 left-to-right임
• Relational operator 및 logical operator의 precedence(우선순위)는 뒤 2.12 참고
2 Types, operators, and expressions(형, 연산자 및 식) 2-30
Logical operator의 계산
2 Types, operators, and expressions(형, 연산자 및 식) 2-31
x y x && y x || y !x
0 (FALSE) 0 (FALSE) 0 (FALSE) 0 (FALSE) 1 (TRUE)
0 (FALSE) 1 (TRUE) 0 (FALSE) 1 (TRUE) 1 (TRUE)
1 (TRUE) 0 (FALSE) 0 (FALSE) 1 (TRUE) 0 (FALSE)
1 (TRUE) 1 (TRUE) 1 (TRUE) 1 (TRUE) 0 (FALSE)
Relational and logical operators - 보기
• Logical operator &&(logical and)로 연결된 식은
left to right 방향으로 계산해 가면서 FALSE가 부분
적으로 계산되면 뒤 부분은 계산하지 않고 값은
FALSE(=0)가 됨
(5 > 10) && (10 > 1) && ... 0 (FALSE)
• Logical operator ||(logical or)로 연결된 식은 left to
right 방향으로 계산해 가면서 TRUE가 부분적으로
계산되면 뒤 부분은 계산하지 않고 값은 TRUE(=1)
가 됨
(10 > 1) || (5 > 10) || ... 1 (TRUE)
2 Types, operators, and expressions(형, 연산자 및 식) 2-32
Relational and logical operators - 보기
• 관계 및 논리 연산자를 사용한 계산 보기
▪ 10 > 20 0 (FALSE)
▪ 10 >= 10 1 (TRUE)
▪ 10 != 20 1 (TRUE)
▪ ((10>20) || (30>20)) + 10 11
2 Types, operators, and expressions(형, 연산자 및 식) 2-33
2.7 Type conversions(형 변환)
• C 언어에서 현 변환에는 아래의 두가지 경우가 있음
①Implicit(암시적) 혹은 automatic(자동) 형 변환: 형 변환하라는 말이 없더라도식을 계산하면서 필요 시 자동으로 수행하는 변환임
②Explicit(명시적) 혹은 coercion(강제) 변환: 프로그램 내에 형을 변환하라고 명시된 부분에서의 형 변환임
2 Types, operators, and expressions(형, 연산자 및 식) 2-34
Implicit(암시적) 혹은 automatic(자동) 형 변환
• 어떤 operator의 operand(피연산자)들이 서로다른 type이면 자동으로 발생함
• 교재의 부록 A6.5에 따라 어떤 operator를 계산하기 전에 operand의 type을 lower type에서higher type으로 변환함 (=narrower to wider promotion)
• 아래 보기에서 1.2는 float 형, 2는 int 형인데float 형이 int 형 보다 higher type이므로 int 2를float 2.0으로 자동 변환한 후 operator +를 수행
1.2 + 2 1.2 + 2.0 3.0
2 Types, operators, and expressions(형, 연산자 및 식) 2-35
Explicit(명시적) 혹은 coercion(강제) 변환
• 프로그램에 형 변환을 명시적으로 표현하고 이 부분을 수행할 때 형을 변환하는 것
• 어떤 식의 형을 명시적으로 변환하라는 표현은 “(형) 식”임
• 아래 보기에서 “(int) 10.9”에서 명시적으로 형 변환을 표현하였기 때문에 이를 수행하여 10을 얻음
(int) 10.9 10
• 아래 보기에서 “(float) (1 + 2)”에서 명시적으로형 변환을 표현하였기 때문에 이를 수행하여 3.0을얻음
(float) (1 + 2) 3.0
2 Types, operators, and expressions(형, 연산자 및 식) 2-36
부록 A6.5 Automatic type conversion
• If 하나의 operand가 long double이면 다른 operand를 long double로변환
• Otherwise, if 하나의 operand가 double이면 다른 operand를 double로변환
• Otherwise, if 하나의 operand가 float이면 다른 operand를 float으로 변환
• Otherwise, short 및 char를, if int에 맞으면 int로 변환하고, otherwiseunsigned int로 변환함(=integral promotion). 그리고 if 하나의 operand가 unsigned long이면 다른 operand를 unsigned long으로 변환
• Otherwise, if 하나의 operand가 long이고 다른 operand가 unsigned int이면, if long에 맞으면 unsigned int를 long으로 변하고, otherwiseunsigned long으로 변환
• Otherwise, if 하나의 operand가 long이면 다른 operand를 long으로 변환
• Otherwise, if 하나의 operand가 unsigned int이면 다른 operand를unsigned int으로 변환
• Otherwise, 두 operand가 int이어야 하며 변한은 하지 않음
2 Types, operators, and expressions(형, 연산자 및 식) 2-37
2.8 Increment and decrement operators(증가 및 감소 연산자)
• Increment 및 decrement operator: ++, --
• Increment 및 decrement operator의 operand는 variable이어야 함 (예: (i+j)++는 오류임)
• Postfix로 사용될 경우의 의미: i 값을 사용한 후, i를 1증가
i = 10;
x = i++; // x에 i(=10)가 저장되고
// i는 1 증가되어 11이 됨
• Prefix로 사용될 경우의 의미: i를 1 증가한 후, i 값을 사용함
i = 10;
x = ++i; // i는 1 증가되어 11이 되고
// x에 i(=11)가 저장됨
2 Types, operators, and expressions(형, 연산자 및 식) 2-38
2.9 Bitwise operators(비트 연산자)
• Bitwise operator(6개)는 아래와 같이 구성됨
& (bitwise AND), | (bitwise inclusive OR), ^ (bitwise exclusive OR), << (left shift), >> (right shift), ~ (1's complement)
• Bitwise & 및 |의 operator의 용도
– bit 0과 &를 하면: 특정 bit을 0으로 clear할 때
– bit 1과 |를 하면: 특정 bit을 1로 set할 때
• Bitwise operator를 사용할 때 operand의 type에 따른bit 수에 주의하여 계산해야 함
– variable i의 type이 int인 경우(=32 bits)인 경우: i = i& 0x7f 수행하면 i의 bit[31] - bit[7]이 모두 0이 됨
2 Types, operators, and expressions(형, 연산자 및 식) 2-39
Bitwise operators의 계산
2 Types, operators, and expressions(형, 연산자 및 식) 2-40
bit x bit y x & y x | y ~x
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
Bitwise operators(비트 연산자) - 보기1
• 아래와 같은 선언이 있을 때
int x = 0xff; // 0000 .... 1111 1111
int y = 0x11; // 0000 .... 0001 0001
int z = 0x88; // 0000 .... 1000 1000
• 아래 bitwise 연산의 계산 값은
▪ x & y 17 (16진수 0x11)
▪ y | z 153 (16진수 0x99)
▪ ~z -137 (16진수 0xffffff77)
2 Types, operators, and expressions(형, 연산자 및 식) 2-41
Bitwise operators(비트 연산자) - 보기2
• 아래와 같은 선언이 있을 때
int x = 0x01; // 0000 .... 0000 0001
int y = 0x80; // 0000 .... 1000 0000
• 아래 bitwise 연산의 계산 값은
▪ x << 2 4 // 0000 .... 0000 0100
▪ y >> 2 32 // 0000 .... 0010 0000
2 Types, operators, and expressions(형, 연산자 및 식) 2-42
2.10 Assignment operators and expressions(지정 연산자와 식)
• Assignment operator는 =임
• Assignment expression의 일반 형식은 다음과 같음
① var = exp
② var op= exp
• 위 ①의 의미는 "exp를 계산하고, 그 값이 var의 새로운 값이되며, 전체 assignment expression의 계산 결과 값이 된다"는의미임
• 위 ②의 의미는 아래 식과 같음 같음
var = var op (exp)
• Assignment operator는 right-to-left associativity(계산 방향)을 가짐
var1 = var2 = exp 는 var1 = (var2 = exp)와 같은 의미임
2 Types, operators, and expressions(형, 연산자 및 식) 2-43
2.11 Conditional operator(조건 연산자)
• Conditional operator는 아래와 같은 형식을 가짐 (주의: 3개의 operand를 가짐)
expr1 ? expr2 : expr3
• 조건 연산자의 의미는 아래와 같음
–만약 expr1을 계산하여 TRUE이면 expr2를 계산한 값이 최종 계산 값이 되고, 그렇지 않으면 exp3를 계산한 값이 최종계산 값이 됨
2 Types, operators, and expressions(형, 연산자 및 식) 2-44
Conditional operator - 보기
• 조건 연산자를 사용한 계산 보기
(20 > 10) ? 100 : 200 100
(10 > 20) ? "yes" : "no" "no"
2 Types, operators, and expressions(형, 연산자 및 식) 2-45
2.12 Precedence and order of evaluation(우선 순위 및 계산 순서)
2 Types, operators, and expressions(형, 연산자 및 식) 2-46
참고: 2장에서 다루지 않은 operator들도 포함됨
precedence높음
^|||||v
precedence낮음
Precedence and order of evaluation - 주의
• Bitwise operator &, ^ 및 | 의 precedence가 relational operator ==의 precedence 보다 낮음에 주의
x & MASK == 0 와 (x & MASK) == 0 는 다름
• 어떤 operator의 operand들의 계산 순서는 정해져 있지않음에 주의
x = f() + g() 에서 함수 f 및 g의 call 순서는 모름
• 함수 call 시 argument의 계산 순서는 정해져 있지 않음에 주의
printf("%d %d\n", ++n, power(2, n)); 애매한 의미
++n; printf("%d %d\n", n, power(2, n));
2 Types, operators, and expressions(형, 연산자 및 식) 2-47
References
[1] Brian W. Kernighan and Dennis M. Ritchie, C
Programming Language, 2nd Edition 2nd
Edition, Prentice Hall, 1988. (Chapter 2)
[2] 김석환 번역, C 언어 프로그래밍 (Kernighan의)
수정판 2판, 출판사 휴먼싸이언스, 2016.
(Chapter 2)
2 Types, operators, and expressions(형, 연산자 및 식) 2-48