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的语法:

链接:https://www.learnfk.comhttps://www.learnfk.com/c-programming/c-preprocessor-if.html

来源:LearnFk无涯教程网

#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

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

技术教程推荐

Service Mesh实践指南 -〔周晶〕

SQL必知必会 -〔陈旸〕

TypeScript开发实战 -〔梁宵〕

浏览器工作原理与实践 -〔李兵〕

Selenium自动化测试实战 -〔郭宏志〕

张汉东的Rust实战课 -〔张汉东〕

李智慧 · 高并发架构实战课 -〔李智慧〕

深入浅出可观测性 -〔翁一磊〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

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