C语言中的条件编译是什么?
C语言中的条件编译是什么?
在C编程语言中,几个指令控制程序代码部分的选择性编译。它们如下所示:
- #if
- #else
- #elif
- #endif
#if 的一般形式如下所示:
#if constant_expression statement sequence #endif
#else 的工作方式与 C 关键字 else 非常相似。
#elif 表示“else if”,并建立了一个 if else-if 编译链。
除其他外,#if 提供了一种“注释掉”代码的替代方法。
例如,
#if 0
printf("#d", total);
#endif
在这里,编译器将忽略 printf("#d", total);
#ifdef 和 #ifndef
#ifdef 表示“如果已定义”,并以 #endif 结尾。
#indef 表示“如果未定义”。
#undef
#undef 删除先前定义的定义。
#line
#line 更改 __LINE__ 的内容,__LINE__ 包含当前编译代码的行号,以及 __FILE__,它是一个包含正在编译的源文件名称的字符串。两者都是编译器中的预定义标识符。
#pragma
#pragma 指令是实现定义的指令,允许向编译器提供各种指令。
示例
以下是 C 程序演示 #ifdef、#ifndef、#else 和 #endif:
# include <stdio.h>
# define a 10
void main(){
#ifdef a
printf("
Hello I am here..");
#endif
#ifndef a
printf("
Not defined ");
#else
printf("
R u There ");
#endif
}
输出
执行上述程序时,会产生以下结果:
Hello I am here.. R u There
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP