배움 저장소

홍정모의 따라하며 배우는 C언어 5 연산자, 표현식, 문장 본문

Programming Language/C

홍정모의 따라하며 배우는 C언어 5 연산자, 표현식, 문장

시옷지읏 2021. 11. 11. 20:01

5.2. 대입 연산자 몇 가지 용어들

Operand & Operator

여기서 i와 1은 operand 피연산자이며  +는 operator 연산자이다.

int i = i+1;

 

 

L-value, R-value

L-value : 메모리 공간을 차지하여 값을 할당받는다

            값을 할당할 수도 있다.

R-value : 값을 할당할 때 사용한다. 계산이 끝나면 사라진다.

int i;     // L-value
i = 1024;  // L-value에 R-value를 할당함
i = i+1;   // L-value는 R-value처럼 쓸 수 있음

 

5.3~5.5 여러 연산자

int i, j, k;
/* Triple Assignment */
i = j = k = 10;
i = (j = (k = 10));

/* Binary Operator(이항연산자) */
3 - 2;

/* Unary Operator (단항연산자) */
-16;


/* 복합 */
-(12-11)

 

5.6 연산자 우선순위와 표현식 트리

결합방식

왼쪽 -> 오른쪽 왼쪽 <- 오른쪽
() =
*/  
+-(이항) +-(단항)

함수 호출시 ()는 Operator, 우선순위를 표시하는 ()는 Primary Expression.

 

5.8 후위연산자

후위연산자(postfix operator) 해당 줄의 코드를 실행한 이후 값을 1 증가시킨다.

int count = 0;
while( count++ < 3 )
{
	printf("%d ", count);   // 출력값 1,2,3
}

count = 0;
while( count < 3 )
{
	printf("%d ", count++); // 출력값 0,1,2
}

후위연산자는 메모리를 가지고 있는 L-value만(const가 아니어야함) 사용할 수 있다.

int x = 1, y = 1;
int z = (x*y)++; // ERROR!

Tip

정리 안 된 식을 복사/붙여넣기하면 Visual Studio가 알아서 정리된 식으로 만들어준다.

 

5.9 표현식과 문장

expression

int c = 0;
printf( "%d \n", 3+(c=1+2) ); // => 6

- 표현식은 연산자와 피연산자의 조합이다.

- 표현식은 값을 반환한다.

- 위 예제의 (c=1+2)도 표현식이다

 

Statements

/*  Statements  */

// declaration statement
int x, y, apples;

// assignment statement
apples = 3;

// null statement
;

// Normal statement
7;
1 + 2;
x = 4;
++x;

// subexpression
x = 1 + (y = 5) // y = 5 is subexpression

// function statement
printf("%d \n", y);

// return statement
return 0;

 

Sequence Points

/* Side Effects and Sequence Points */
x = 4;  // = operator's main intent is evaluating 
        //   expressions. assignment is side effects
        
        
while(x++ < 10) // (x++ < 10)은 완전한 문장 full expression이다.
{               // 따라서 (x < 10)이 실행되고 후위연산자를 연산한다
	printf("%d \n", x);
}

Sequence Points는 statement(문장)이 끝나는 곳이다. ' ; ' 도 Sequence Point이다.  ' , ' Comma도 Sequence Point이다. 후위 연산자는 문장이 끝나고나서 실행된다. 그러니 Sequence Point 이후에 후위연산자가 실행된다.

( Sequence Points : ' ; 'Semicolon  ' , 'Comma '&&'And '||'OR )

 

5.11 자료형 변환

/* promotions in assignments */
short s = 64;
int i = s;

float f = 3.14f;
double d = f;

/* demotion in assignments */
d = 1.25;
f = 1.25f;  // 0.25 = 1/4은 double, float 모두 표현 가능하다

d = 1.123;
f = 1.123f; // truncated: float = double
           // 0.123은 2진수로 표현할 때 긴 자료형일수록 정밀하다

double 1.25와 float 1.25f는 정확하게 표현 가능하다.
double 1.123과 float 1.123은 모두 근사치로 표현하고 있다. 정밀도가 높으면 유리하다

 

/* ranking of types in operations */
d = f + 1.234; // double <= double + float
f = f + 1.234; // Warrnings, truncated : float <= double + float

- 실수 > 정수 : 실수와 정수가 함께 연산하면
                    정수를 실수로 변환하여 계산한다

- 자료형 크기가 크면 우선순위가 높다

 

직접 형 변환하기

double d = (double)3.14f;
int i = 1.6 + 1.7;        // i=3
i = (int)1.6 + 1.7;       // i=2

자료형을 어떻게 사용하느냐에 따라 값이 달라질 수 있다.

 

char c1 = 83.99 // => 'S' = 83

/* demolition */
char c2 = 1106; // => 'R' = 82

 

0b100001010010 = 1106
0b      01010010 =    82  = 'R' = 1106 % 256

 

char 자료형에 1106값을 넣으면 앞에 있는 bit data는 모두 날라간다.

이때 연산결과는 자료형 최대값을 나누어준 나머지와 동일하다.

 

5.12 함수의 인수와 매개변수

Argument : value (함수를 호출할 때 넣는 값, 인수)

Parameter : variables (함수를 정의할 때 가상의 값, 매개변수)

Comments