Lập trình C - C #if

Chỉ thị tiền xử lý #if

Chỉ thị tiền xử lý #if đánh giá biểu thức hoặc điều kiện. Nếu điều kiện là đúng, nó thực thi mã nếu không thực thi mã trong  #elseif hoặc #else hoặc #endif được thực thi..

Cách 1:

#if bieu_thuc  
//code  
#endif  

Cách 2:

#if bieu_thuc  
//if code  
#else  
//else code  
#endif  

Cách 3:

#if bieu_thuc  
//if code  
#elif bieu_thuc  
//elif code  
#else  
//else code  
#endif  

Ví dụ #if:

#include <stdio.h>  
#include <conio.h>  
#define NUMBER 0  
void main() {  
#if (NUMBER==0)  
printf("Value of Number is: %d",NUMBER);  
#endif         
getch();  
}  

Kết quả:

Value of Number is: 0

Xem thêm ví dụ về #if

#include <stdio.h>    
#include <conio.h>    
#define NUMBER 1  
void main() {  
clrscr();  
#if (NUMBER==0)  
printf("1 Value of Number is: %d",NUMBER);  
#endif  
  
#if (NUMBER==1)  
printf("2 Value of Number is: %d",NUMBER);  
#endif  
getch();  
}  

Kết quả:

2 Value of Number is: 1