Ch5. C...
Transcript of Ch5. C...
1
Intelligent Robotics Lab.
Ch5. C 언어의 기본 연산자
Topics and Objectives다양한 종류의 연산자(operator)
대입 연산자(=)와 산술 연산자(+,-,*,/,%)를 통하여 보는 연산의 원리
복합(compound) 대입 연산자
부호 연산자
증가, 감소 연산자
관계 연산자
논리 연산자
콤마(,) 연산자
연산자의 우선순위와 결합방향
자료형 변환 연산자
sizeof 연산자
2
Intelligent Robotics Lab.
산술 연산자를 통해서 보는 연산의 원리
이항 연산자(binary operator)
두 개의 피연산자를 필요로 하는 연산자
대입 연산자와 산술 연산자는 모두 이항 연산자임.
3
Intelligent Robotics Lab.
대입 연산자와 산술 연산자의 사용 예: ex) 5-1.c, pp.96-97
4
Intelligent Robotics Lab.
printf 문에 존재하는 연산식의 처리 과정
연산식이 먼저 처리가 되고, 그 결과로 얻어진 값을 기반으로 재 구성
된 printf 문이 실행됨: 그림 5-1
5
Intelligent Robotics Lab.
연산결과의 자료형과 피연산자의 자료형연산 방식은 피연산자의 자료형에 따라서 결정
두 개의 피연산자가 정수이면, 정수형 나눗셈 진행
두 개의 피연산자가 실수이면, 실수형 나눗셈 진행
int main(void){
int n1=7;int n2=3;. . .printf("나눗셈 결과: %d \n", n1/n2);. . . // 연산결과 2
}
int main(void){
double n1=7.0;double n2=3.0;. . .printf("나눗셈 결과: %g \n", n1/n2);. . . // 연산결과 2.333..
}
6
Intelligent Robotics Lab.
두 자료형이 일치하지 않는 경우자료형의 일치 및 데이터의 손실을 최소화하기 위하여 표현범위가 넓은 형태로 형 변환이 자동으로 진행됨.
int main(void){
int n1=7;double n2=1.245;double result= n1 + n2;. . . .
}
변수 n1에 저장된 값 7이double형 실수 7.0으로 변환
7
Intelligent Robotics Lab.
% 연산자의 피연산자
% 연산자는 나머지를 반환하는 연산자이고, 피연산자가 반드시 정수
이어야 함.
int main(void){
double n1=10.0;double n2= 2.0;int mod= n1 % n2;. . . .
}
실수형 데이터가 % 연산자의 피연산자이므로 컴파일오류 발생
8
Intelligent Robotics Lab.
연산자들의 우선순위와 결합방향
연산자의 우선순위와 결합방향: 표 5-2, p.102연산자의 우선순위
연산자의 연산 순서를 결정하는 순위
연산자의 결합방향
동일한 연산자 사이의 연산 진행 방향
→ 는 결합방향이 왼쪽에서 오른쪽으로 이동
← 는 결합방향이 오른쪽에서 왼쪽으로 이동
9
Intelligent Robotics Lab.
연산의 연산 순서를 직접 지정할 때, 소괄호 사용연산자의 우선순위나 결합방향에 상관없이 실행순서를 지정할 때 사용
int num1 = 5-(3-2); // num1=4
int num2 = 5-3-2; // num2=0
10
Intelligent Robotics Lab.
다양한 연산자들의 소개
복합(compound) 대입 연산자
대입 연산자와 다른 연산자의 결합하여 정의된 연산자를 의미함
: 그림 5-3, 그림 5-4
11
Intelligent Robotics Lab.
복합(compound) 대입 연산자의 활용 예ex) 5-2.c
#include <stdio.h>
int main(void){
int n1 = 7, n2=5;
n1 += n2; //n1=n1+n2;printf("n1 += n2: %d \n", n1);
n2 +=12; //n2=n2+12;printf("n2 += 12: %d \n", n2);return 0;
}
12
Intelligent Robotics Lab.
부호 연산자로서의 +와 -+, - 단항 연산자(unary operator: 피연산자가 하나인 것)-는 부호를 바꾸는 역할
+는 별다른 기능을 제공하지 않음. 단지 연산자로 정의하여 에러를 발생시키지 않도록 컴파일을 허용: ex) 5-3.c
int main(void){
int n=5;printf("현재의 n: %d \n", n);printf("-n: %d, +n: %d \n\n", -n, +n);
n = -n;printf("변경된 n: %d \n", n);printf("-n: %d, +n: %d \n\n", -n, +n);return 0;
}
13
Intelligent Robotics Lab.
prefix 증가, 감소 연산자 (++, --)
prefix (접두사): 피연산자의 앞에 붙는 연산자를 의미함.
값을 1 증가 및 감소 시키는 연산자
연산의 결과가 바로 반영되어서 나머지 연산을 진행
14
Intelligent Robotics Lab.
prefix 증가, 감소 연산자 (++, --) 의 활용 예
ex) 5-4.c
#include <stdio.h>
int main(void){
int num1 = 7;int num2, num3;
num2 = ++num1;num3 = --num1;
printf("num1: %d \n", num1);printf("num2: %d \n", num2);printf("num3: %d \n", num3);
return 0;}
15
Intelligent Robotics Lab.
postfix 증가, 감소 연산자 (++, --)
postfix (접미사): 피연산자의 뒤에 붙는 연산자를 의미함.
값을 1 증가 및 감소 시키는 연산자
연산의 결과는 다음 문장에 반영이 됨.
16
Intelligent Robotics Lab.
postfix 증가, 감소 연산자 (++, --) 의 활용 예
ex) 5-5.c
#include <stdio.h>
int main(void){
int num1 = 7;int num2, num3;
num2 = num1++;num3 = num1--;
printf("num1: %d \n", num1);printf("num2: %d \n", num2);printf("num3: %d \n", num3);
return 0;}
17
Intelligent Robotics Lab.
prefix ++와 postfix ++의 차이점
값이 증가되는 시기의 차이 : ex) 5-6.c, pp. 110-111
18
Intelligent Robotics Lab.
관계 연산자: 표 5-5대소 비교 : <, >, <=, >=동등 비교 : ==, !=관계 연산자는 연산식이 참이면 1, 거짓이면 0을 반환
19
Intelligent Robotics Lab.
관계 연산자의 활용 예: ex) 5-7.c
#include <stdio.h>
int main(void){
int num1=10;int num2=12;
int result1, result2;
result1 = (num1 == num2); // 동등 비교result2 = (num1 <= num2); // 대소 비교
printf("같은가? %d \n", result1);printf("같거나 작은가? %d \n", result2);
return 0;}
20
Intelligent Robotics Lab.
C 언어에서의 참(true)과 거짓(false)C 언어에서는 0을 ‘거짓’을 의미하는 숫자로, 0이 아닌 모든 수를 ‘참’을 의미하는 숫자로 정의
참과 거짓을 따져주는 연산자들은 연산의 결과로 0 또는 0이 아닌 값(사실상 1)을 반환
논리 연산자참과 거짓을 따지는 연산자
AND(논리곱), OR(논리합), NOT(논리부정)을 의미하는 연산자로 구성
21
Intelligent Robotics Lab.
논리 연산자의 진리표(truth table)
22
Intelligent Robotics Lab.
논리 연산자의 활용 예: ex) 5-8.c
int main(void){
int num1 = 10;int num2 = 12;
int result1, result2, result3;
result1 = (num1==10 && num2==12);result2 = (num1<=12 || num2>=12);result3 = !num1;
printf("result1 : %d \n", result1);printf("result2 : %d \n", result2);printf("result3 : %d \n", result3);return 0;
}
23
Intelligent Robotics Lab.
콤마(,) 연산자둘 이상의 변수를 동시에 선언, 둘 이상의 함수 호출문, 연산문을 하나의 문장으로 표현하는 경우에 사용
int main(void){
int a=1, b=2;
a++, b++, printf("postfix ++ 연산 실행\n");printf("a: %d, ", a), printf("b: %d \n", b);
return 0;}
24
Intelligent Robotics Lab.
자료형 변환 연산자(cast operator)자료형을 변환시키는 연산자
ex) 5-10.c
int main(void){
printf("정수출력: %f \n", (double)3 );
printf("실수출력: %d \n", (int)3.14 );return 0;
}
소수점 이하는 정수로 표현불가능, 따라서 3.14를 int형데이터로 변환 시 소수부가잘려나가 정수 3이 됨
25
Intelligent Robotics Lab.
sizeof 연산자피 연산자의 크기를 바이트 단위로 계산해서 반환하는 연산자
ex) 5-11.c, pp.119-120
int main(void){
int num = 1234;printf("size of num: %d \n\n", sizeof(num));
printf("size of 1234: %d \n", sizeof(1234));printf("size of 1234.0: %d \n", sizeof(1234.0));printf("size of 0.1234: %d \n\n", sizeof(0.1234));
printf("size of char: %d \n", sizeof(char));printf("size of short: %d \n", sizeof(short));printf("size of double: %d \n\n", sizeof(double));return 0;
}
26
Intelligent Robotics Lab.
Home WorkPage 100에 있는 문제 5-1을 해결하라.