Preprocessor = โปรแกรมประมวลผลก่อน compile เริ่มด้วย #
3 ประเภทหลัก
- Macro substitution - #define
- File inclusion - #include
- Compiler control - #ifdef, #ifndef, #else, #endif
1. Macro Substitution
#define PI 3.1415
#define SQUARE(x) x*x
#undef PI // ยกเลิก macro
ตัวอย่าง
#define wait getch()
main() {
clrscr();
printf("Hello");
wait;
}
2. File Inclusion
#include <stdio.h> // system header
#include "myheader.h" // user header
3. Compiler Control (Conditional Compilation)
#define LINE 1
#include <stdio.h>
main() {
#ifdef LINE
printf("This is line number one");
#else
printf("This is line number two");
#endif
}
ผลลัพธ์
This is line number one