독립적으로 실행하거나 C/C++ 컴파일 툴체인 내에서 활용할 수 있는 C 전처리기(cpp)의 핵심 문법 퀵 레퍼런스.
독립 실행형 전처리
전체 컴파일러를 호출하지 않고 전처리기만 단독 실행:
# -P 플래그는 줄 번호 마커 생성을 억제합니다
cpp -P source.c > preprocessed.c
헤더 파일 포함 (Header Inclusions)
#include <stdio.h> /* 시스템 및 표준 라이브러리 헤더 */
#include "my_header.h" /* 프로젝트 로컬 헤더 */
매크로 정의
#define BUFFER_SIZE 1024
#define PI 3.1415926535
/* 매크로 정의 해제 */
#undef BUFFER_SIZE
함수형 매크로
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define DEG_TO_RAD(x) ((x) * 0.0174532925)
문자열화 연산자 (#)
단일 # 연산자는 매크로 인수를 문자열 리터럴로 변환합니다:
#define STR(name) #name
#define PRINT_VAR(x) printf("%s = %d\n", #x, x)
char *var_name = STR(my_object); /* → "my_object" */
토큰 결합 연산자 (##)
이중 ## 연산자는 두 토큰을 하나의 새로운 토큰으로 결합합니다:
#define COMMAND(NAME) { #NAME, NAME ## _command }
struct command commands[] = {
COMMAND(quit), /* → { "quit", quit_command } */
COMMAND(help) /* → { "help", help_command } */
};
조건부 컴파일 (Conditional Compilation)
#ifdef DEBUG
#define LOG(msg) printf("[DEBUG] %s\n", msg)
#elif defined(VERBOSE)
#define LOG(msg) printf("[INFO] %s\n", msg)
#else
#define LOG(msg) /* noop */
#endif
/* 중복 포함 방지 가드 (Include guards) */
#ifndef MY_HEADER_H
#define MY_HEADER_H
/* 선언문 작성 */
#endif /* MY_HEADER_H */
진단 및 컴파일 경고
#if __STDC_VERSION__ < 201112L
#warning "C11 features are not fully enabled"
#endif
#if !defined(REQUIRED_CONFIG)
#error "REQUIRED_CONFIG must be defined before compilation"
#endif
표준 사전 정의 매크로
__FILE__: 현재 소스 파일 이름 문자열.__LINE__: 현재 코드 줄 번호 정수.__DATE__: 컴파일 날짜 문자열 (Mmm dd yyyy).__TIME__: 컴파일 시간 문자열 (hh:mm:ss).