Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

Pre-processor Commands ในภาษา C: Macro, Include, Conditional

Preprocessor = โปรแกรมประมวลผลก่อน compile เริ่มด้วย #

3 ประเภทหลัก

  1. Macro substitution - #define
  2. File inclusion - #include
  3. 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