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

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

技术教程推荐

持续交付36讲 -〔王潇俊〕

技术管理实战36讲 -〔刘建国〕

MySQL实战45讲 -〔林晓斌〕

WebAssembly入门课 -〔于航〕

基于人因的用户体验设计课 -〔刘石〕

性能优化高手课 -〔尉刚强〕

去无方向的信 -〔小麥〕

网络排查案例课 -〔杨胜辉〕

Serverless进阶实战课 -〔静远〕

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