博学而笃志 切问而近思 仁在其中
详情
C语言#ifndef指令
作者:--     发布时间:2019-11-20     评论:0     阅读:0

#ifndef预处理程序指令检查宏是否为未由#define定义。如果是,则执行代码,否则#else代码执行(如果存在)。

语法:

#ifndef macro  
    //code  
#endif

#else语法:

#ifndef macro  
    //successful code  
#else  
    //else code  
#endif

#ifndef示例

下面来看看一个简单使用#ifndef预处理指令的例子。创建一个源文件:ifndef-example.c,其代码实现如下 -

#include <stdio.h>  

#define input  
void main() {
    int a = 0;
#ifndef input  
    a = 2;
#else  
    printf("enter a:");
    scanf("%d", &a);
#endif         
    printf("value of a: %d\n", a);
}

执行上面示例代码,得到以下结果 -

enter a:200
value of a: 200

但是,如果不定义input,它将执行#ifndef的代码块。创建一个源文件:ifndef-example2.c,其代码实现如下 -

#include <stdio.h>  

void main() {
    int a = 0;
#ifndef input  
    a = 2;
#else  
    printf("enter a:");
    scanf("%d", &a);
#endif         
    printf("value of a: %d\n", a);
}

执行上面示例代码,得到以下结果 -

value of a: 2


下一篇:测试
相关文章
loading......
最新动态
所有评论

loading......

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册