SlideShare uma empresa Scribd logo
1 de 15
Baixar para ler offline
Preprocessor
Preprocessor,
math.h
stdlib.h
2
Preprocessor
 Preprocessor
– #로 시작되는 모든 statement의 처리
– 대표적으로 #define 과 #include가 있음.
[Ex] #include <stdio.h>
#define PI 3.14159
3
Preprocessor
[Ex]
#include <stdio.h>
#include “./test/file.h”
현재폴더 아래에 존재하는 test폴더 안에
있는 file.h를 포함시켜라.
컴파일러가 제공하는 stdio.h를 포함시켜라
C가 제공하는 Standard library를 사용하기
위해서는 stdio.h를 포함시켜야 함.
 #include <파일> 혹은 #include “파일”
– 파일을 source 파일의 현 위치에 포함시켜라. (source 파일이 변경
되는 것이 아니라, 컴파일 직전 preprocessor가 만드는 임시 파일에
include되고 그것이 컴파일 됨)
– <>는 파일이 사전에 지정된 기본 폴더에 있는 경우 사용
(Compiler가 기본 제공하는 header file은 대부분인 기본 폴더에 있
있음)
– “”는 파일이 그 외 폴더에 존재할 때 사용 (파일이 현재 폴더에 없으
면 경로까지 적어 주어야 함)
Preprocessor
 Header file
– #include에 의해 포함시키는 파일은 *.h로 끝나는 header file
들임
 Header file에 포함 되는 내용
– Function의 prototype
– Global 변수의 extern 선언
– 필요한 type definition 등
 대표적 header file
– stdio.h, stdlib.h, math.h 등
4
15주차 Modulization 참고
5
Preprocessor
[Ex]
#define LIMIT 100
#define PI 3.14159
 #define A B
– “B를 A로도 간주해라”라는 의미임
– “혹은 프로그램에 나오는 모든 A를 B로 대체시켜라”의 의미
프로그램 내에 LIMIT를 100과 같은 의미로,
PI는 3.141592와 같은 의미로 사용하겠다.
6
Preprocessor
#include <stdio.h>
#define LIMIT 100
#define PI 3.14159
int main(void)
{
printf( “%d, %fn”, LIMIT, PI ) ;
}
 #define
…
int main(void)
{
printf( “%d, %fn”, 100, 3.14159 ) ;
}
Preprocessor가 아래와 같은 임시 파일을
생성하고, 이 임시파일이 컴파일되어
실행파일이 생성된다.
7
Preprocessor
 Example
#include <stdio.h>
#define YELLOW 0
#define RED 1
#define BLUE 2
int main(void)
{
int color ;
for( color = YELLOW ; color <= BLUE ; color++ ) {
switch( color ) {
case YELLOW : printf( “Yellown” ) ; break ;
case RED : printf( “Redn” ) ; break ;
case BLUE : printf( “Bluen” ) ; break ;
}
}
return 0;
}
8
Preprocessor
 매크로 함수: #define을 이용한 함수 정의
– 실행시 아래와 같이 Preprocessor에 의해 코드가 변환 된 뒤 컴파일 된다.
#define multiply(a,b) ((a)*(b))
void main() {
int c = multiply(3,2);
return 0;
}
…
void main() {
int c = ((3)*(2));
return 0;
}
9
Preprocessor
 매크로 함수: 아래와 같이 정의하면 어떨까?
– 실행시 아래와 같이 Preprocessor에 의해 코드가 변환 되면..
– 매크로 함수를 정의할 때 ()를 사용해야 안전하다.
#define multiply(a,b) a*b
void main() {
int c = multiply(3+1,2+2);
return 0;
}
…
void main() {
int c = 3+1*2+2;
return 0;
}
Math functions
(math.h)
11
Mathematical Functions
 Mathematical Functions
– sqrt(), pow(), exp(), log(), sin(), cos(), tan()등….
– Header file <math.h>의 삽입 필요
– 모든 math function은 double type의 argument와 double type
의 return value를 갖는다.
– Unix에서 Compile시 math library를 link하기 위해 “-lm”을 옵션
으로 주어야 한다.
[Ex] gcc filename.c -lm
[Ex] double sin(double); /* 모든 angle은 radian으로 표시 */
double pow(double, double);
12
Mathematical Functions
[Ex] #include <stdio.h>
#include <math.h>
#define PI 3.1415926
int main() {
double r = PI / (2*90); /* 1도에 해당하는 radian값 */
int i;
for(i=0; i<=90; i+=5) {
printf("cos(%d) = %ft", i, cos(r*i));
printf("sin(%d) = %ft", i, sin(r*i));
printf("tan(%d) = %fn", i, tan(r*i));
}
return 0;
}
cos(0) = 1.000000 sin(0) = 0.000000 tan(0) = 0.000000
…
cos(45) = 0.707107 sin(45) = 0.707107 tan(45) = 1.000000
…
cos(90) = 0.000000 sin(90) = 1.000000 tan(90) = 37320539.634355
기타 functions
(stdlib.h)
14
기타 유용한 함수들
 기타 Functions
– atoi(), rand(), srand()
– 위와 같은 함수를 사용하려면 <stdlib.h> 삽입 필요
– atoi(“숫자스트링”): 주어진 숫자스트링을 숫자로 변환하여 반환
– rand() : 랜덤값을 반환
– srand( 초기값 ) : rand()함수 초기화
int a = atoi( “1234” ) ;
int a = rand() ;
srand( 3 ) ;
 예제: 10개의 랜덤 값을 출력하기
 실행할 때 마다 다른 랜덤 값을 출력하게 하려면
15
기타 유용한 함수들
#include <stdio.h>
#include <stdlib.h>
int main() {
for(i=0; i<10; i++)
printf(“%dn”, rand() );
}
이 프로그램을
두 번 실행해 보자
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand( (int)time(NULL) );
for(i=0; i<10; i++)
printf(“%dn”, rand() );
}

Mais conteúdo relacionado

Mais procurados

Boost 라이브리와 C++11
Boost 라이브리와 C++11Boost 라이브리와 C++11
Boost 라이브리와 C++11OnGameServer
 
Multi mechanize
Multi mechanizeMulti mechanize
Multi mechanizeSungMin OH
 
파이썬 스터디 9장
파이썬 스터디 9장파이썬 스터디 9장
파이썬 스터디 9장SeongHyun Ahn
 
Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기지수 윤
 
Adv sys prog_20123186_report1
Adv sys prog_20123186_report1Adv sys prog_20123186_report1
Adv sys prog_20123186_report1준석 김
 
Function calling convention
Function calling conventionFunction calling convention
Function calling conventionYuk SeungChan
 
Ch.14 파일 강c v0.6
Ch.14 파일 강c v0.6Ch.14 파일 강c v0.6
Ch.14 파일 강c v0.6승태 김
 
PyCon 12월 세미나 - 실전 파이썬 프로그래밍 책 홍보
PyCon 12월 세미나 - 실전 파이썬 프로그래밍 책 홍보PyCon 12월 세미나 - 실전 파이썬 프로그래밍 책 홍보
PyCon 12월 세미나 - 실전 파이썬 프로그래밍 책 홍보Young Hoo Kim
 
파이썬 함수
파이썬 함수파이썬 함수
파이썬 함수Gibson Kim
 
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5현웅 김
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
Dependency hell과 빌드지옥 탈출
Dependency hell과 빌드지옥 탈출Dependency hell과 빌드지옥 탈출
Dependency hell과 빌드지옥 탈출Byeongsu Kang
 
Data Structure 3
Data Structure 3Data Structure 3
Data Structure 3yonsei
 
모두의 JIT 컴파일러
모두의 JIT 컴파일러모두의 JIT 컴파일러
모두의 JIT 컴파일러우경 성
 

Mais procurados (20)

skku cp2 w4
skku cp2 w4skku cp2 w4
skku cp2 w4
 
Boost 라이브리와 C++11
Boost 라이브리와 C++11Boost 라이브리와 C++11
Boost 라이브리와 C++11
 
Multi mechanize
Multi mechanizeMulti mechanize
Multi mechanize
 
파이썬 스터디 9장
파이썬 스터디 9장파이썬 스터디 9장
파이썬 스터디 9장
 
파이선 실전공략-1
파이선 실전공략-1파이선 실전공략-1
파이선 실전공략-1
 
Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기
 
Adv sys prog_20123186_report1
Adv sys prog_20123186_report1Adv sys prog_20123186_report1
Adv sys prog_20123186_report1
 
Function calling convention
Function calling conventionFunction calling convention
Function calling convention
 
Hello world
Hello worldHello world
Hello world
 
Ch.14 파일 강c v0.6
Ch.14 파일 강c v0.6Ch.14 파일 강c v0.6
Ch.14 파일 강c v0.6
 
Stack frame
Stack frameStack frame
Stack frame
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
PyCon 12월 세미나 - 실전 파이썬 프로그래밍 책 홍보
PyCon 12월 세미나 - 실전 파이썬 프로그래밍 책 홍보PyCon 12월 세미나 - 실전 파이썬 프로그래밍 책 홍보
PyCon 12월 세미나 - 실전 파이썬 프로그래밍 책 홍보
 
파이썬 함수
파이썬 함수파이썬 함수
파이썬 함수
 
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
C++11
C++11C++11
C++11
 
Dependency hell과 빌드지옥 탈출
Dependency hell과 빌드지옥 탈출Dependency hell과 빌드지옥 탈출
Dependency hell과 빌드지옥 탈출
 
Data Structure 3
Data Structure 3Data Structure 3
Data Structure 3
 
모두의 JIT 컴파일러
모두의 JIT 컴파일러모두의 JIT 컴파일러
모두의 JIT 컴파일러
 

Destaque

[Gpg2권 박민근] 2.8 프랙탈의 프로그래밍
[Gpg2권 박민근] 2.8 프랙탈의 프로그래밍[Gpg2권 박민근] 2.8 프랙탈의 프로그래밍
[Gpg2권 박민근] 2.8 프랙탈의 프로그래밍MinGeun Park
 
NDC2011 - 절차적 지형과 트렌드의 추적자들
NDC2011 - 절차적 지형과 트렌드의 추적자들NDC2011 - 절차적 지형과 트렌드의 추적자들
NDC2011 - 절차적 지형과 트렌드의 추적자들Jubok Kim
 
Question 5 Math 1
Question 5 Math 1Question 5 Math 1
Question 5 Math 1M.T.H Group
 
[2015.06] 수학GPS - 5분 스피치 발표 자료
[2015.06]   수학GPS - 5분 스피치 발표 자료[2015.06]   수학GPS - 5분 스피치 발표 자료
[2015.06] 수학GPS - 5분 스피치 발표 자료Ji Yong Kim
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 

Destaque (6)

[Gpg2권 박민근] 2.8 프랙탈의 프로그래밍
[Gpg2권 박민근] 2.8 프랙탈의 프로그래밍[Gpg2권 박민근] 2.8 프랙탈의 프로그래밍
[Gpg2권 박민근] 2.8 프랙탈의 프로그래밍
 
NDC2011 - 절차적 지형과 트렌드의 추적자들
NDC2011 - 절차적 지형과 트렌드의 추적자들NDC2011 - 절차적 지형과 트렌드의 추적자들
NDC2011 - 절차적 지형과 트렌드의 추적자들
 
Question 5 Math 1
Question 5 Math 1Question 5 Math 1
Question 5 Math 1
 
[2015.06] 수학GPS - 5분 스피치 발표 자료
[2015.06]   수학GPS - 5분 스피치 발표 자료[2015.06]   수학GPS - 5분 스피치 발표 자료
[2015.06] 수학GPS - 5분 스피치 발표 자료
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 
Build Features, Not Apps
Build Features, Not AppsBuild Features, Not Apps
Build Features, Not Apps
 

Semelhante a 3 1. preprocessor, math, stdlib

C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2Chris Ohk
 
파이썬2.7 기초 공부한 것 정리
파이썬2.7 기초 공부한 것 정리파이썬2.7 기초 공부한 것 정리
파이썬2.7 기초 공부한 것 정리Booseol Shin
 
Debian packaging - Advanced
Debian packaging - AdvancedDebian packaging - Advanced
Debian packaging - Advanced경섭 심
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features SummaryChris Ohk
 
RNC C++ lecture_2 operator, if
RNC C++ lecture_2 operator, ifRNC C++ lecture_2 operator, if
RNC C++ lecture_2 operator, ifitlockit
 
RNC C++ lecture_4 While, For
RNC C++ lecture_4 While, ForRNC C++ lecture_4 While, For
RNC C++ lecture_4 While, Foritlockit
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉HyunJoon Park
 
Git: A Motivating Introduction
Git: A Motivating IntroductionGit: A Motivating Introduction
Git: A Motivating IntroductionJongwook Choi
 
C#을 사용한 빠른 툴 개발
C#을 사용한 빠른 툴 개발C#을 사용한 빠른 툴 개발
C#을 사용한 빠른 툴 개발흥배 최
 
리눅스 커널 기초 태스크관리
리눅스 커널 기초 태스크관리리눅스 커널 기초 태스크관리
리눅스 커널 기초 태스크관리Seungyong Lee
 
Google Protocol buffer
Google Protocol bufferGoogle Protocol buffer
Google Protocol bufferknight1128
 
자료구조 Project1
자료구조 Project1자료구조 Project1
자료구조 Project1KoChungWook
 

Semelhante a 3 1. preprocessor, math, stdlib (20)

HI-ARC PS 101
HI-ARC PS 101HI-ARC PS 101
HI-ARC PS 101
 
Basic git-commands
Basic git-commandsBasic git-commands
Basic git-commands
 
C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2
 
6 function
6 function6 function
6 function
 
파이썬2.7 기초 공부한 것 정리
파이썬2.7 기초 공부한 것 정리파이썬2.7 기초 공부한 것 정리
파이썬2.7 기초 공부한 것 정리
 
Debian packaging - Advanced
Debian packaging - AdvancedDebian packaging - Advanced
Debian packaging - Advanced
 
06장 함수
06장 함수06장 함수
06장 함수
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary
 
Python - Module
Python - ModulePython - Module
Python - Module
 
RNC C++ lecture_2 operator, if
RNC C++ lecture_2 operator, ifRNC C++ lecture_2 operator, if
RNC C++ lecture_2 operator, if
 
RNC C++ lecture_4 While, For
RNC C++ lecture_4 While, ForRNC C++ lecture_4 While, For
RNC C++ lecture_4 While, For
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉
 
Git: A Motivating Introduction
Git: A Motivating IntroductionGit: A Motivating Introduction
Git: A Motivating Introduction
 
C#을 사용한 빠른 툴 개발
C#을 사용한 빠른 툴 개발C#을 사용한 빠른 툴 개발
C#을 사용한 빠른 툴 개발
 
리눅스 커널 기초 태스크관리
리눅스 커널 기초 태스크관리리눅스 커널 기초 태스크관리
리눅스 커널 기초 태스크관리
 
pgday-2023
pgday-2023pgday-2023
pgday-2023
 
04 프로세스
04 프로세스04 프로세스
04 프로세스
 
Google Protocol buffer
Google Protocol bufferGoogle Protocol buffer
Google Protocol buffer
 
자바모델 클래스에 날개를달자_롬복(Lombok)
자바모델 클래스에 날개를달자_롬복(Lombok)자바모델 클래스에 날개를달자_롬복(Lombok)
자바모델 클래스에 날개를달자_롬복(Lombok)
 
자료구조 Project1
자료구조 Project1자료구조 Project1
자료구조 Project1
 

Mais de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료웅식 전
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)웅식 전
 

Mais de 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
 

3 1. preprocessor, math, stdlib

  • 2. 2 Preprocessor  Preprocessor – #로 시작되는 모든 statement의 처리 – 대표적으로 #define 과 #include가 있음. [Ex] #include <stdio.h> #define PI 3.14159
  • 3. 3 Preprocessor [Ex] #include <stdio.h> #include “./test/file.h” 현재폴더 아래에 존재하는 test폴더 안에 있는 file.h를 포함시켜라. 컴파일러가 제공하는 stdio.h를 포함시켜라 C가 제공하는 Standard library를 사용하기 위해서는 stdio.h를 포함시켜야 함.  #include <파일> 혹은 #include “파일” – 파일을 source 파일의 현 위치에 포함시켜라. (source 파일이 변경 되는 것이 아니라, 컴파일 직전 preprocessor가 만드는 임시 파일에 include되고 그것이 컴파일 됨) – <>는 파일이 사전에 지정된 기본 폴더에 있는 경우 사용 (Compiler가 기본 제공하는 header file은 대부분인 기본 폴더에 있 있음) – “”는 파일이 그 외 폴더에 존재할 때 사용 (파일이 현재 폴더에 없으 면 경로까지 적어 주어야 함)
  • 4. Preprocessor  Header file – #include에 의해 포함시키는 파일은 *.h로 끝나는 header file 들임  Header file에 포함 되는 내용 – Function의 prototype – Global 변수의 extern 선언 – 필요한 type definition 등  대표적 header file – stdio.h, stdlib.h, math.h 등 4 15주차 Modulization 참고
  • 5. 5 Preprocessor [Ex] #define LIMIT 100 #define PI 3.14159  #define A B – “B를 A로도 간주해라”라는 의미임 – “혹은 프로그램에 나오는 모든 A를 B로 대체시켜라”의 의미 프로그램 내에 LIMIT를 100과 같은 의미로, PI는 3.141592와 같은 의미로 사용하겠다.
  • 6. 6 Preprocessor #include <stdio.h> #define LIMIT 100 #define PI 3.14159 int main(void) { printf( “%d, %fn”, LIMIT, PI ) ; }  #define … int main(void) { printf( “%d, %fn”, 100, 3.14159 ) ; } Preprocessor가 아래와 같은 임시 파일을 생성하고, 이 임시파일이 컴파일되어 실행파일이 생성된다.
  • 7. 7 Preprocessor  Example #include <stdio.h> #define YELLOW 0 #define RED 1 #define BLUE 2 int main(void) { int color ; for( color = YELLOW ; color <= BLUE ; color++ ) { switch( color ) { case YELLOW : printf( “Yellown” ) ; break ; case RED : printf( “Redn” ) ; break ; case BLUE : printf( “Bluen” ) ; break ; } } return 0; }
  • 8. 8 Preprocessor  매크로 함수: #define을 이용한 함수 정의 – 실행시 아래와 같이 Preprocessor에 의해 코드가 변환 된 뒤 컴파일 된다. #define multiply(a,b) ((a)*(b)) void main() { int c = multiply(3,2); return 0; } … void main() { int c = ((3)*(2)); return 0; }
  • 9. 9 Preprocessor  매크로 함수: 아래와 같이 정의하면 어떨까? – 실행시 아래와 같이 Preprocessor에 의해 코드가 변환 되면.. – 매크로 함수를 정의할 때 ()를 사용해야 안전하다. #define multiply(a,b) a*b void main() { int c = multiply(3+1,2+2); return 0; } … void main() { int c = 3+1*2+2; return 0; }
  • 11. 11 Mathematical Functions  Mathematical Functions – sqrt(), pow(), exp(), log(), sin(), cos(), tan()등…. – Header file <math.h>의 삽입 필요 – 모든 math function은 double type의 argument와 double type 의 return value를 갖는다. – Unix에서 Compile시 math library를 link하기 위해 “-lm”을 옵션 으로 주어야 한다. [Ex] gcc filename.c -lm [Ex] double sin(double); /* 모든 angle은 radian으로 표시 */ double pow(double, double);
  • 12. 12 Mathematical Functions [Ex] #include <stdio.h> #include <math.h> #define PI 3.1415926 int main() { double r = PI / (2*90); /* 1도에 해당하는 radian값 */ int i; for(i=0; i<=90; i+=5) { printf("cos(%d) = %ft", i, cos(r*i)); printf("sin(%d) = %ft", i, sin(r*i)); printf("tan(%d) = %fn", i, tan(r*i)); } return 0; } cos(0) = 1.000000 sin(0) = 0.000000 tan(0) = 0.000000 … cos(45) = 0.707107 sin(45) = 0.707107 tan(45) = 1.000000 … cos(90) = 0.000000 sin(90) = 1.000000 tan(90) = 37320539.634355
  • 14. 14 기타 유용한 함수들  기타 Functions – atoi(), rand(), srand() – 위와 같은 함수를 사용하려면 <stdlib.h> 삽입 필요 – atoi(“숫자스트링”): 주어진 숫자스트링을 숫자로 변환하여 반환 – rand() : 랜덤값을 반환 – srand( 초기값 ) : rand()함수 초기화 int a = atoi( “1234” ) ; int a = rand() ; srand( 3 ) ;
  • 15.  예제: 10개의 랜덤 값을 출력하기  실행할 때 마다 다른 랜덤 값을 출력하게 하려면 15 기타 유용한 함수들 #include <stdio.h> #include <stdlib.h> int main() { for(i=0; i<10; i++) printf(“%dn”, rand() ); } 이 프로그램을 두 번 실행해 보자 #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand( (int)time(NULL) ); for(i=0; i<10; i++) printf(“%dn”, rand() ); }