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() );
}