19강 전처리기

YU YU·2021년 9월 27일
0
post-thumbnail

전처리기

다른 프로그램 영역과 독립적으로 처리됨
전처리기 구문은 소스코드 파일 단위로 효력 존재

프로그램을 컴파일할 때 컴파일 직전에 실행되는 별도의 프로그램. 전처리기가 실행되면 각 코드에서 #자로 시작하는 지시자를 찾는다.

18-1. 파일 포함 전처리기

  • #include <파일 이름>
    컴파일러와 함께 제공되는 파일을 include 할 때 사용

  • #include "파일 이름"
    같은 디렉토리 위치 상의 파일을 include할 때 사용. 보통 자신이 직접 만든 헤더파일.

18-2. 직접 만든 헤더파일 적용

//temp.h
int add(int a, int b){
return a+b\}
#include <stdio.h>
#include "temp.h"

int main(void){
	int a = 10, b = 20;
    printf(%d\bn",add(a,b));
    system('pause');
    return 0;
}

#include를 이용해 가져오는 팡릴은 파일의 소스코드 자체를 다 가져오는 것.
그래서 한번 참조한 파일을 여러번 참조하지 않도록 해야함.

18-3. 매크로 전처리기

#define 문법을 사용해 정의할 수 있다.

#include <stdio.h>
#define PI 3.13159

int main(void){
	int r = 10;
    printf("원의 둘레: %.2f\n",2*PI*r);
    system("pause");
    return 0;
}

인자도 포함될 수 있다.

#include <stdio.h>
#define POW(x) (x*x)

int main(void){
	int r = 10;
    printf("r의 제곱: %d\n",POW(r));
    system("pause");
    return 0;
}

#define은 소스코드 양을 획기적으로 줄이는데 도움을 준다.

#include <stdio.h>
#define ll long long
#define ld long double

int main(void){
	ll a =98765432100000;
    ld b =100.5432
    printf("%.2f\n",a*b);
    system("pause");
    return 0;
}

18-4.조건부 컴파일

조건부 컴파일은 헤더 파일의 내용이 중복되어 사용하지 않도록 하기 위해서 적용함.
특정 맥크로가 이미 선언이 되어있지 않을 때에만 특정 구문을 컴파일함

형식
#ifndef
소스코드
#endif

파일 분할 컴파일

일반적로 직접 라이브러리를 만들 때에는 C언어 파일과 헤더 파일을 모두 작성해야 한다.

//temp.h
#ifndef _TEMP_H_
#def _TEMP_H_
int add(int a, int b);
#endif

//temp.c
#include "temp.h"
int add(int a, int b){
	return a+b;
}

//main.c
#include <stdio.h>
#include "temp.h"

int main(void){
	printf("%d\n",add(3,5));
    system("puase");
    return 0;
}
profile
코딩 재밌어요!

0개의 댓글

관련 채용 정보