首页 > 其他分享 >C语言如果用-D定义了一个宏AAA,那么#if AAA的结果是多少

C语言如果用-D定义了一个宏AAA,那么#if AAA的结果是多少

时间:2024-01-19 14:57:11浏览次数:31  
标签:AAA 定义 C语言 pragma ifdef fail ok message

目录

参考资料

Preprocessor Options (Using the GNU Compiler Collection (GCC))
条件编译#ifdef的妙用详解_透彻_ifdef多个条件-CSDN博客

验证

直接用源码验证是最好的

源码

点击查看代码
#include <stdio.h>
// command: gcc -DAAA -DBBB=1 -DCCC=2 -DDDD=0 cMacroIfAndMacroIfdef.c
int main(int argc, char** argv)
{
#ifdef AAA
#pragma message("#ifdef AAA ok")
        printf("#ifdef AAA ok\n");
#else
#pragma message("#ifdef AAA fail")
        printf("#ifdef AAA fail\n");
#endif

#if AAA
#pragma message("#if AAA ok")
        printf("#if AAA ok\n");
#else
#pragma message("#if AAA fail")
        printf("#if AAA fail\n");
#endif

#if AAA == 0
#pragma message("#if AAA == 0 ok")
        printf("#if AAA == 0 ok\n");
#else
#pragma message("#if AAA == 0 fail")
        printf("#if AAA == 0 fail\n");
#endif

#if AAA == 1
#pragma message("#if AAA == 1 ok")
        printf("#if AAA == 1 ok\n");
#else
#pragma message("#if AAA == 1 fail")
        printf("#if AAA == 1 fail\n");
#endif

#if AAA == 2
#pragma message("#if AAA == 2 ok")
        printf("#if AAA == 2 ok\n");
#else
#pragma message("#if AAA == 2 fail")
        printf("#if AAA == 2 fail\n");
#endif

#ifdef BBB == 1
#pragma message("#ifdef BBB == 1 ok")
        printf("#ifdef BBB == 1 ok\n");
#else
#pragma message("#if BBB == 2 fail")
        printf("#if AAA ==2 fail\n");
#endif

#if BBB == 0
#pragma message("#if BBB == 0 ok")
        printf("#if BBB == 0 ok\n");
#else
#pragma message("#if BBB == 0 fail")
        printf("#if BBB == 0 fail\n");
#endif

#if BBB == 1
#pragma message("#if BBB == 1 ok")
        printf("#if BBB == 1 ok\n");
#else
#pragma message("#if BBB == 1 fail")
        printf("#if BBB == 1 fail\n");
#endif

#if BBB == 2
#pragma message("#if BBB == 2 ok")
        printf("#if BBB == 2 ok\n");
#else
#pragma message("#if BBB == 2 fail")
        printf("#if BBB == 2 fail\n");
#endif

#ifdef CCC
#pragma message("#ifdef CCC ok")
        printf("#ifdef CCC ok\n");
#else
#pragma message("#ifdef CCC fail")
        printf("#ifdef CCC fail\n");
#endif
#if CCC == 0
#pragma message("#if CCC == 0 ok")
        printf("#if CCC == 0 ok\n");
#else
#pragma message("#if CCC == 0 fail")
        printf("#if CCC == 0 fail\n");
#endif
#if CCC == 1
#pragma message("#if CCC == 1 ok")
        printf("#if CCC == 1 ok\n");
#else
#pragma message("#if CCC == 1 fail")
        printf("#if CCC == 1 fail\n");
#endif
#if CCC == 2
#pragma message("#if CCC == 2 ok")
        printf("#if CCC == 2 ok\n");
#else
#pragma message("#if CCC == 2 fail")
        printf("#if CCC == 2 fail\n");
#endif

#ifdef DDD
#pragma message("#ifdef DDD ok")
        printf("#ifdef DDD ok\n");
#else
#pragma message("#ifdef DDD fail")
        printf("#ifdef DDD fail\n");
#endif
#if DDD == 0
#pragma message("#if DDD == 0 ok")
        printf("#if DDD == 0 ok\n");
#else
#pragma message("#if DDD == 0 fail")
        printf("#if DDD == 0 fail\n");
#endif
#if DDD == 1
#pragma message("#if DDD == 1 ok")
        printf("#if DDD == 1 ok\n");
#else
#pragma message("#if DDD == 1 fail")
        printf("#if DDD == 1 fail\n");
#endif
#if DDD == 2
#pragma message("#if DDD == 2 ok")
        printf("#if DDD == 2 ok\n");
#else
#pragma message("#if DDD == 2 fail")
        printf("#if DDD == 2 fail\n");
#endif

#ifdef EEE
#pragma message("#ifdef EEE ok")
        printf("#ifdef EEE ok\n");
#else
#pragma message("#ifdef EEE fail")
        printf("#ifdef EEE fail\n");
#endif
#if EEE == 0
#pragma message("#if EEE == 0 ok")
        printf("#if EEE == 0 ok\n");
#else
#pragma message("#if EEE == 0 fail")
        printf("#if EEE == 0 fail\n");
#endif
#if EEE == 1
#pragma message("#if EEE == 1 ok")
        printf("#if EEE == 1 ok\n");
#else
#pragma message("#if EEE == 1 fail")
        printf("#if EEE == 1 fail\n");
#endif
#if EEE == 2
#pragma message("#if EEE == 2 ok")
        printf("#if EEE == 2 ok\n");
#else
#pragma message("#if EEE == 2 fail")
        printf("#if EEE == 2 fail\n");
#endif
        return 0;
}%

编译效果

> gcc -DAAA -DBBB=1 -DCCC=2 -DDDD=0 cMacroIfAndMacroIfdef.c
cMacroIfAndMacroIfdef.c: In function ‘main’:
cMacroIfAndMacroIfdef.c:7:9: note: ‘#pragma message: #ifdef AAA ok’
    7 | #pragma message("#ifdef AAA ok")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:15:9: note: ‘#pragma message: #if AAA ok’
   15 | #pragma message("#if AAA ok")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:26:9: note: ‘#pragma message: #if AAA == 0 fail’
   26 | #pragma message("#if AAA == 0 fail")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:31:9: note: ‘#pragma message: #if AAA == 1 ok’
   31 | #pragma message("#if AAA == 1 ok")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:42:9: note: ‘#pragma message: #if AAA == 2 fail’
   42 | #pragma message("#if AAA == 2 fail")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:46:12: warning: extra tokens at end of #ifdef directive
   46 | #ifdef BBB == 1
      |            ^~
cMacroIfAndMacroIfdef.c:47:9: note: ‘#pragma message: #ifdef BBB == 1 ok’
   47 | #pragma message("#ifdef BBB == 1 ok")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:58:9: note: ‘#pragma message: #if BBB == 0 fail’
   58 | #pragma message("#if BBB == 0 fail")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:63:9: note: ‘#pragma message: #if BBB == 1 ok’
   63 | #pragma message("#if BBB == 1 ok")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:74:9: note: ‘#pragma message: #if BBB == 2 fail’
   74 | #pragma message("#if BBB == 2 fail")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:79:9: note: ‘#pragma message: #ifdef CCC ok’
   79 | #pragma message("#ifdef CCC ok")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:89:9: note: ‘#pragma message: #if CCC == 0 fail’
   89 | #pragma message("#if CCC == 0 fail")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:96:9: note: ‘#pragma message: #if CCC == 1 fail’
   96 | #pragma message("#if CCC == 1 fail")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:100:9: note: ‘#pragma message: #if CCC == 2 ok’
  100 | #pragma message("#if CCC == 2 ok")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:108:9: note: ‘#pragma message: #ifdef DDD ok’
  108 | #pragma message("#ifdef DDD ok")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:115:9: note: ‘#pragma message: #if DDD == 0 ok’
  115 | #pragma message("#if DDD == 0 ok")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:125:9: note: ‘#pragma message: #if DDD == 1 fail’
  125 | #pragma message("#if DDD == 1 fail")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:132:9: note: ‘#pragma message: #if DDD == 2 fail’
  132 | #pragma message("#if DDD == 2 fail")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:140:9: note: ‘#pragma message: #ifdef EEE fail’
  140 | #pragma message("#ifdef EEE fail")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:144:9: note: ‘#pragma message: #if EEE == 0 ok’
  144 | #pragma message("#if EEE == 0 ok")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:154:9: note: ‘#pragma message: #if EEE == 1 fail’
  154 | #pragma message("#if EEE == 1 fail")
      |         ^~~~~~~
cMacroIfAndMacroIfdef.c:161:9: note: ‘#pragma message: #if EEE == 2 fail’
  161 | #pragma message("#if EEE == 2 fail")
      |         ^~~~~~~

运行效果

> ./a.out
#ifdef AAA ok
#if AAA ok
#if AAA == 0 fail
#if AAA == 1 ok
#if AAA == 2 fail
#ifdef BBB == 1 ok
#if BBB == 0 fail
#if BBB == 1 ok
#if BBB == 2 fail
#ifdef CCC ok
#if CCC == 0 fail
#if CCC == 1 fail
#if CCC == 2 ok
#ifdef DDD ok
#if DDD == 0 ok
#if DDD == 1 fail
#if DDD == 2 fail
#ifdef EEE fail
#if EEE == 0 ok
#if EEE == 1 fail
#if EEE == 2 fail

标签:AAA,定义,C语言,pragma,ifdef,fail,ok,message
From: https://www.cnblogs.com/SendBoringBackToNoWhere/p/17974617

相关文章

  • (8)Powershell中变量的定义和使用
    (8)Powershell中变量的定义和使用这一节主要介绍Powershell中变量的定义和使用,以及使用变量时应该注意的事项。和所有编程语言中的变量一样,Powershell中的变量也是存于存储值的内存单元,需要注意的是,Powershell中的变量是以美元符号($)开头的单字节(一般是英文字符,虽然ISE中支持......
  • 数独Sudoku游戏解题C语言和Golang(Go语言)实现
    Go语言实现packagemainimport( "fmt" "os")const( N=9 EmptyCell='0')funcmain(){ iflen(os.Args)!=2||len(os.Args[1])!=81{ fmt.Println("错误:程序需要一个正好包含81位数字的参数。") os.Exit(1) } boa......
  • STM32CubeMX教程18 DAC - DMA输出自定义波形
    1、准备材料开发板(正点原子stm32f407探索者开发板V2.4)STM32CubeMX软件(Version6.10.0)野火DAP仿真器keilµVision5IDE(MDK-Arm)ST-LINK/V2驱动一台示波器逻辑分析仪nanoDLA2、实验目标使用STM32CubeMX软件配置STM32F407开发板的DACOUT1实现输出0-3.3V周期为12.8ms的正弦......
  • C语言爬虫采集图书网站百万数据
    最近需要查阅一些资料,只给到相关项目名称以及关键词,想通过图书文库找到对应书籍,那么怎么才能在百万数据库中找到自己需要的文献呢?今天我依然用C语言写个爬虫程序,从百万数据库中查找到适合的文章,能节省很多事情。下面是一个简单的C#爬虫程序,它使用了HttpWebRequest和HttpWebResponse......
  • @RestControllerAdvice定义返回格式
    原文链接:如何优雅的写Controller层代码?一、拦截异常,封装返回值@RestControllerAdvicepublicclassControllerExceptionAdvice{@ExceptionHandler({BindException.class})publicResultVoMethodArgumentNotValidExceptionHandler(BindExceptione){/......
  • 用户自定义模板中数据区域
    在实际的Word文档开发中,经常需要自动填充数据到Word模板中,以生成动态的Word文档,那么应该如何编辑制作Word模板呢?方法一:直接打开Word文件插入书签假如使用数据区域(DataRegion)来定义模板中动态填充数据的位置,那么直接打开一个Word文件,在其中添加“PO_”开头的书签即可制作word模板......
  • HarmonyOS4.0系列——03、声明式UI、链式编程、事件方法、以及自定义组件简单案例
    HarmonyOS4.0系列——03、声明式UI、链式编程、事件方法、以及自定义组件简单案例声明式UIArkTS以声明方式组合和扩展组件来描述应用程序的UI,同时还提供了基本的属性、事件和子组件配置方法,帮助开发者实现应用交互逻辑。如果组件的接口定义没有包含必选构造参数,则组件后面的“()”......
  • MFC 自定义控件添加 tip
    一、要求1.自定义tip提示并实时提示,跟随鼠标移动2.避免重复刷导致tip闪烁3.picture控件添加自定义变量二、tip涉及2.1CMFCToolTipCtrl、CMFCToolTipInfoCMFCToolTipCtrl*m_ptooltip;//注册控件、传递提示消息CMFCToolTipInfo*m_ttinfo;//样式处理......
  • vue+antd-vue(自定义iconfont图标组件)
    1.方式一代码如下import{createFromIconfontCN}from'@ant-design/icons-vue';constIconFont=createFromIconfontCN({scriptUrl:newURL('./assets/font/iconfont.js',import.meta.url).href});app.component('IconFont',IconFont);......
  • Win10如何设置闹钟提醒?win10电脑自定义闹钟提醒的方法
    上班族在繁忙的工作中,有时候需要在电脑上设置醒闹钟提醒,以确保按时完成工作或者提醒自己关注某些事项。想想一下,你在某一天需要参加一个重要的会议,为了不错过会议的开始时间,设置一个电脑上的醒闹钟提醒是十分必要的。此外上班族通常有各种工作任务需要按时完成,这就需要分阶段提醒......