C语言 - #if

C语言 - #if 首页 / C语言入门教程 / C语言 - #if

#if预处理程序指令评估表达式或条件。如果condition为true,则执行代码,否则执行#elseif或#else或#endif代码。

语法:

#if expression
//code
#endif

#else的语法:

#if expression
//if code
#else
//else code
#endif

#elif和#else的语法:

#if expression
//if code
#elif expression
//elif code
#else
//else code
#endif

C #if示例

让我们看一个使用#if预处理程序指令的简单示例。

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

输出:

Value of Number is: 0

让我们看另一个示例,以清楚地理解#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();  
}  

输出:

2 Value of Number is: 1

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

推荐系统三十六式 -〔刑无刀〕

从0开始学大数据 -〔李智慧〕

说透中台 -〔王健〕

DDD实战课 -〔欧创新〕

SRE实战手册 -〔赵成〕

如何落地业务建模 -〔徐昊〕

大型Android系统重构实战 -〔黄俊彬〕

云时代的JVM原理与实战 -〔康杨〕

互联网人的数字化企业生存指南 -〔沈欣〕

好记忆不如烂笔头。留下您的足迹吧 :)