Upload
sigourney-tyler
View
52
Download
0
Embed Size (px)
DESCRIPTION
2. chapter. Lexical Elements, Operators, and the C System. 내 용. Characters and Lexical Elements Comments Keywords Identifiers Constants String Constants. 내 용. Operators and Punctuators Precedence and Associativity of Operators Increment and Decrement Operator Assignment Operators - PowerPoint PPT Presentation
Citation preview
2. Lexical Elements, Operators, and the C System
전자정보공학부
Lexical Elements, Operators,and the C System
chapter 2
2
2. Lexical Elements, Operators, and the C System
전자정보공학부
내 용
Characters and Lexical Elements
Comments
Keywords
Identifiers
Constants
String Constants
3
2. Lexical Elements, Operators, and the C System
전자정보공학부
내 용
Operators and Punctuators
Precedence and Associativity of Operators
Increment and Decrement Operator
Assignment Operators
the C System
Style
4
2. Lexical Elements, Operators, and the C System
전자정보공학부
Characters and Lexical Elements
컴파일러– 언어의 문법을 확인하는 프로그램– 전처리기가 먼저 작동하므로 전처리기도 컴파일러의 일부로 간주– 프로그램을 이루는 문자들을 토큰으로 모아 구별
토큰 (tokens)– C 언어에서 사용되는 어휘 요소들– 키워드 , 식별자 , 상수 , 문자열 상수 , 연산자 , 구두점
5
2. Lexical Elements, Operators, and the C System
전자정보공학부
Characters and Lexical Elements
프로그램에서 사용할 수 있는 문자
Characters that can be used in a program
Lowercases letters a b c d . . . z
Uppercase letters A B C D . . . Z
Digits 0 1 2 3 4 5 6 7 8 9
Other characters+ - * / = ( ) { } [ ] < > ’” !
@ # $ % ^ & _ | \ . , ; : ?
White space characters
공백 (space), 개행문자 (form feed),
탭 (tab) 등
6
2. Lexical Elements, Operators, and the C System
전자정보공학부
Characters and Lexical Elements
token 의 종류 – identifier : letters, digits, ‘_’ 로 구성된 token
– operator : 연산자
– punctuator : { } , ; ( )
– keyword : c 에서 특별한 의미를 갖는 예약어
Token 사이의 공백문자 (white space)– 허용되며 적절한 공백문자의 사용으로 보다 프로그램
이해력을 향상
Space, tab, new line
7
2. Lexical Elements, Operators, and the C System
전자정보공학부
Comments
/* 와 */ 사이에 있는 문장 Compiler 에 의해 무시되는 문장 프로그램의 수행과는 무관 프로그램 흐름의 이해를 높이기 위한 문장 프로그램 작성자 , 작성일 , 목적 , 설명 등을 표시 주석문의 예
/* a commnet */ /*** another comment **/ /*****/ /* * A comment ca be written in this fashion * to set it off from the surrounding code. */
/* a commnet */ /*** another comment **/ /*****/ /* * A comment ca be written in this fashion * to set it off from the surrounding code. */
/*************************** * If you wish, you can * * put comments in a box. * ***************************/
/*************************** * If you wish, you can * * put comments in a box. * ***************************/
8
2. Lexical Elements, Operators, and the C System
전자정보공학부
Keywords
하나의 token 으로 간주되며 program 에서 특별한 의미를 갖는 예약어
Keywords
auto do goto signed unsigned
break double if sizeof void
case else int static volatile
char enum long struct while
const extern register switch
continue float return typedef
default for short union
9
2. Lexical Elements, Operators, and the C System
전자정보공학부
Identifiers
함수명 , 변수명 등을 말한다 영문자 (a-z, A-Z), 숫자 (0-9), underscore (_) 로 구성 영문자 나 underscore(_) 로 시작 대 , 소문자 구별 255 자 까지 가능 , 그러나 첫 31 자 까지만 인식 Keywords 는 사용될 수 없다
10
2. Lexical Elements, Operators, and the C System
전자정보공학부
C 에서 사용할 수 없는 식별자의 예
인식되는 식별자의 길이– ANSI C : 31 자
– C 컴파일러와 운영체제에 따라 8 자까지 인식되는 경우도 있음
Identifiers
not#me /* 특수문자 # 는 사용할 수 없다 */ 101_south /* 식별자는 숫자로 시작할 수 없다 */ -pius /* ‘_’ 를 ‘ -’ 로 실수하기 쉽다 */
not#me /* 특수문자 # 는 사용할 수 없다 */ 101_south /* 식별자는 숫자로 시작할 수 없다 */ -pius /* ‘_’ 를 ‘ -’ 로 실수하기 쉽다 */
11
2. Lexical Elements, Operators, and the C System
전자정보공학부
Constants
정수형 상수 (Integer Constants)– 8 진수 , 10 진수 , 16 진수로 표현
실수형 상수 (Float Constants)– 부동소수점 상수라고도 함 .
– 6 장에서 자세히… 상수의 예
0 /*10 진 상수 */77 /*10 진 상수 */ 0123 /* 8 진 상수 */ -49 /* 상수 수식 */ 123.0 /* 부동소수점형 상수 */
0 /*10 진 상수 */77 /*10 진 상수 */ 0123 /* 8 진 상수 */ -49 /* 상수 수식 */ 123.0 /* 부동소수점형 상수 */
12
2. Lexical Elements, Operators, and the C System
전자정보공학부
String Constants
문자형 상수 (Character Constants)– 단일 따옴표 (’) 로 둘러 싸인 문자
문자열 상수 (String Constants)– 이중 따옴표 (“”) 로 둘러 싸인 문자– Compiler 에 의해 배열 (Array) 형태로 저장– Character constants 와 구분 예
‘a’ /* 문자 */“a” /* 문자열 */
‘a’ /* 문자 */“a” /* 문자열 */
13
2. Lexical Elements, Operators, and the C System
전자정보공학부
공백에 의해 구분되는 두개의 문자열은 compiler 에 의해 단일문자열로 간주
String Constants
"abc" "def" 는 ” abcdef" 와 같다 ."abc" "def" 는 ” abcdef" 와 같다 .
14
2. Lexical Elements, Operators, and the C System
전자정보공학부
Operators and Punctuators
산술 연산자의 예
구두점– 괄호 (()), 중괄호 ({}), 쉼표 (,), 세미콜론 (;)
– main 뒤에 오는 괄호는 연사자처럼 취급됨 Unary Operators
– +, - : 양수 , 음수 표현– 오른쪽에서 왼쪽방향으로 계산
+ - * / %+ - * / %나머지 연산
15
2. Lexical Elements, Operators, and the C System
전자정보공학부
Operators and Punctuators
이항연산자 (Binary operator)– 왼쪽에서 오른쪽 방향으로 계산
– + , - , * , / , %
– *, /, % 가 +, - 보다 우선순위가 높다 .
– 왼쪽에서 오른쪽 방향으로 계산
– % 연산자• 나머지를 계산• Operand 의 type 이 정수가 아닌 경우 error
• 배수 구하기 등에 사용
5%2=2 -7%2=-1
5%2=2 -7%2=-1
16
2. Lexical Elements, Operators, and the C System
전자정보공학부
Precedence and Associativity of Operators
연산자 결합순서
( ) ++(postfix) --(postfix)
+(unary) -(unary) ++(prefix) --(prefix)
* / %
+ -
= += -= *= /= etc.
1 + 2 - 3 + 4 - 5 는 (((1 +3) - 3) + 4) - 5 와 같다 . 1 + 2 - 3 + 4 - 5 는 (((1 +3) - 3) + 4) - 5 와 같다 .
17
2. Lexical Elements, Operators, and the C System
전자정보공학부
증가 연산자Postfix k++
Prefix ++k
감소 연산자 Postfix k--
Prefix --k
Increment and Decrement Operators
k=k+1
k=k-1
Prefix : operand 가 사용되기 전에 먼저 증가 / 감소 시킴 Postfix : operand 가 사용된 후에 증가 / 감소 시킴 Prefix : operand 가 사용되기 전에 먼저 증가 / 감소 시킴 Postfix : operand 가 사용된 후에 증가 / 감소 시킴
18
2. Lexical Elements, Operators, and the C System
전자정보공학부
틀린 사용 예
선언 / 초기화와 예
Increment and Decrement Operators
777++; ++(a * b - 1) ; 777++; ++(a * b - 1) ;
int a = 1, b = 2, c = 3, d = 4;
수식 등가수식 값 a * b / c 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
19
2. Lexical Elements, Operators, and the C System
전자정보공학부
Compiler 는 = 을 연산자로 인식
– variable 을 위한 메모리 공간에 right_side 의 값이 할당된다 모든 연산자 중에서 우선순위가 가장 낮다 . 오른쪽에서 왼쪽으로의 결합성을 갖는다 예
Assignment Operators
variable = right_side variable = right_side
b = 2; c = 3; a = b + c; /* a = (b = 2) + (c = 3) */ a = b = c = 0; /* a = ( b = (c = 0) ) ) */
b = 2; c = 3; a = b + c; /* a = (b = 2) + (c = 3) */ a = b = c = 0; /* a = ( b = (c = 0) ) ) */
20
2. Lexical Elements, Operators, and the C System
전자정보공학부
대입 연산자의 종류
예
Assignment Operators
= += -= *= / = %= >>= <<= &= ^= |= = += -= *= / = %= >>= <<= &= ^= |=
int k = 5;k += 2; /* k = k + 2, k=7 */k -= 2; /* k = k - 2, k=5 */k *= 2; /* k = k * 2, k=10 */k /= 2; /* k = k / 2, k=5 */k %= 2; /* k = k % 2, k=1 */
int k = 5;k += 2; /* k = k + 2, k=7 */k -= 2; /* k = k - 2, k=5 */k *= 2; /* k = k * 2, k=10 */k /= 2; /* k = k / 2, k=5 */k %= 2; /* k = k % 2, k=1 */
j *= k +3 은 j = j * (k +3) 과 동일 , j = j * k + 3 과는 다름
21
2. Lexical Elements, Operators, and the C System
전자정보공학부
대입문이 계산되는 과정을 보여주는 예
Assignment Operators
int i = 1, j = 2, k = 3, m = 4;
수식 등가수식 등가수식 값 i += j +k i += (j +k) i = (i + (j +k)) 6
j *= k = m + 5
j *= (k = (m +5)) j = (j * (k = (m +5)))
18
22
2. Lexical Elements, Operators, and the C System
전자정보공학부
예제 : 2 의 멱승
/* Some powers of 2 are printed. */
#include <stdio.h> int main(void) { int exponent = 0, power_of_tow = 1;
while (++exponent <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0; }
/* Some powers of 2 are printed. */
#include <stdio.h> int main(void) { int exponent = 0, power_of_tow = 1;
while (++exponent <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0; }
2 4 8 16 32 64 128 256 512 1024
23
2. Lexical Elements, Operators, and the C System
전자정보공학부
전처리기 (Preprocessor)– # 로 시작되는 줄은 전처리기 명령
– 전처리기 명령은 컴파일러가 아닌 전처리기에 의해 처리됨
– 예
#define
– compile 전에 symbol 을 모두 value 로 바꾸어 줌
The C System
#include <stdio.h> #define PI 3.14159#include "filename"
#include <stdio.h> #define PI 3.14159#include "filename"
#define symbol value#define symbol value
24
2. Lexical Elements, Operators, and the C System
전자정보공학부
#include– 컴파일 전에 주어진 파일의 복사본을 include 문 대신 삽입
– 삽입되는 파일은 헤더파일 (header file) 로서 확장자명은 ‘ ~.h’
– 헤더파일의 주된 목적 :
• 함수의 프로토타입 (prototype) 을 제공하기 위함
– stdio.h 에 들어있는 내용 예
The C System
int printf(const char *format, ...); int scanf(const char *format, ...); int printf(const char *format, ...); int scanf(const char *format, ...);
25
2. Lexical Elements, Operators, and the C System
전자정보공학부
표준 라이브러리 (The Standard Library)– 많은 유용한 함수 제공으로 다양하고 강력한 C system 의
구현 가능
– 보다 신속한 수행을 위해 컴파일된 함수 제공
– 제공되는 함수 사용을 위해 함수 호출문과 그 함수를 호출하기 위한 함수 프로토타입의 제공 필요
• 표준 라이브러리 : 컴파일된 함수들로 구성 .
• 표준 헤더 파일 : 주로 함수 프로토타입들을 포함하는 파일로서 라이브러리 함수를 사용하기 위해서는 그 함수 프로토타입이 정의된 헤더파일의 include 가 필요
The C System
26
2. Lexical Elements, Operators, and the C System
전자정보공학부
예제 : 난수 ( 정수형 ) 출력/* Printing random numbers. */
#include <stdio.h> #include <stdlib.h>
int main(void) { int i, n; printf("\n%s\n%s", "Some randoml distributed integers will be printed.", "How many do you want to see? "); scanf("%d", &n); for (i=0; I < n; ++i) { if (i % 6 == 0) printf("\n"); printf("%9d", rand()); } printf("\n"); return 0; }
/* Printing random numbers. */
#include <stdio.h> #include <stdlib.h>
int main(void) { int i, n; printf("\n%s\n%s", "Some randoml distributed integers will be printed.", "How many do you want to see? "); scanf("%d", &n); for (i=0; I < n; ++i) { if (i % 6 == 0) printf("\n"); printf("%9d", rand()); } printf("\n"); return 0; }
rand() 함수는 0~32767 사이의 난수( 정수 ) 를 return 해주는 library 함수로 이 함수의 prototype 은 stdlib.h 에 정의됨
27
2. Lexical Elements, Operators, and the C System
전자정보공학부
예제 : 난수 ( 정수형 ) 출력
• 11 을 입력했을 경우
Some randomly distributed integers will be printed. How many do you want to see? 11 16838 5758 10113 17515 31051 5627 23010 7419 16212 4086 2749
28
2. Lexical Elements, Operators, and the C System
전자정보공학부
Style
Space 의 절약 보다 Readability 가 중요y = 2;
z = 3; x = ( y = 2) + (z = 3);
x = y + z;
Compound Assignment 의 선호a += 7; a = a + 7;
program readability 의 향상을 위해 주석 삽입 필요– Program 의 작성일 , 작성자 , program 의 목적 , block 단위
또는 line 단위의 수행 방법 등을 반드시 표시