Skip to content
Snippets Groups Projects
Commit 2311d707 authored by 이장원's avatar 이장원
Browse files

Update README.md

parent 3f4c1aba
Branches
No related tags found
No related merge requests found
...@@ -63,6 +63,110 @@ $ ./test >> output_data ...@@ -63,6 +63,110 @@ $ ./test >> output_data
$ ./test < intput_data $ ./test < intput_data
``` ```
# C Preprocessor
전처리기(preprocessor)는 전처리(.c -> .i)를 해주는 도구이다. 전처리는 .h에 있는 텍스트들을 .c에 치환(붙여넣기)해주는 과정이다. C Preprocessor는 CPP로 줄여쓰기도 한다.
### Including files
* `#include <filename>`
표준 헤더 파일을 include하는 방식으로 표준 라이브러리들이 모여있는 폴더에서 헤더 파일을 검색해서 include한다.
* `#include "filename"`
사용자 헤더 파일을 include하는 방식으로 현재 소스 파일이 위치한 폴더에서 헤더 파일을 검색해서 include한다.
### Conditional compilation
#if, #ifdef, #ifndef, #else, #elif, #endif는 조건부 컴파일을 할 수 있게 한다. 조건이 맞지 않으면 그 부분이 컴파일에서 제외된다. 즉, 소스 자체가 없는 효과가 있다.
#error을 사용하여 컴파일을 실패하게 할 수도 있다.
### Macro definition
특정 숫자, 함수, 프로그램 블록을 다른 형태로 변환 지정한다. 변환은 원래 한 줄만 가능하지만 \를 사용하여 여러 줄을 연결할 수 있다.
* `#define <identifier> <replacement token list>`
대상변환형(Object-like macros)이라고 하며 identifier를 replacement token list로 변환한다.
* `#define <identifier>(<parameter list>) <replacement token list>`
유사함수변환형(Function-like macros)이라고 하며 identifier와 parameter list를 replacement token list로 변환한다.
* `#undef <identifier>`
#undef를 사용해 정의된 매크로를 무효화할 수도 있다.
```c
#define f(a) a*a
b = f(20+13) // b = 293
```
전처리는 단순히 텍스트를 치환하는 것이기 때문에 위의 코드를 실행할 시
```c
b = 20 + 13 * 20 + 13
```
이렇게 치환되므로 괄호를 최대한 많이 써주는 것이 좋다.
```c
#define f(a) ((a)*(a))
b = f(20+13) // b = 1089
```
### etc.
* #pragma once
`#pragma once`를 사용하여 여러번 include하는 것을 방지할 수 있다.
* Token stringification
`#define <identifier>(<parameter>) #parameter`
#연산자는 token을 문자열 상수로 변환한다.
* Token concatenation
`#define <identifier>(<parameter>) parameter##string`
##연산자는 두 token을 하나로 연결하여 변환한다.
* Debuging macros
디버깅을 하면서 어느 부분에서 에러가 났는 지 알려준다.
* __FILE\_\_
에러가 발생한 파일 경로
* __LINE\_\_
에러가 발생한 라인
* __FUNCTION\_\_
에러가 발생한 함수
# Multiple file compile
* func.h
```
#define SQAURE(a) ((a)*(a))
extern int func(int a);
```
* func.c
```
#include "func.h"
int func(int a)
{
return(a * 10);
}
```
* main.c
```
#include <stdio.h>
#include "func.h"
int main()
{
printf("func(100): %d SQUARE(10): %d\n", func(100), SQUARE(10));
return 0;
}
```
보통 이런 식으로 코드를 작성하고
```
$ gcc main.c func.c
```
이렇게 컴파일, 링크하여 실행 파일을 실행한다.
# 컴파일러 vs 인터프리터 # 컴파일러 vs 인터프리터
컴파일러와 인터프리터는 고급언어로 작성된 원시 프로그램(Source Program)을 목적 프로그램(Object Program)으로 번역하는 번역 프로그램이며, 프로그램 번역 방식에 따라 구분된다. 컴파일러와 인터프리터는 고급언어로 작성된 원시 프로그램(Source Program)을 목적 프로그램(Object Program)으로 번역하는 번역 프로그램이며, 프로그램 번역 방식에 따라 구분된다.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment