`
vyloy
  • 浏览: 78679 次
  • 性别: Icon_minigender_1
  • 来自: 佛山
社区版块
存档分类
最新评论

C++宏#号的用法

    博客分类:
  • C++
阅读更多
1. #:在宏展开的时候会将#后面的参数替换成字符串,如:
          #define p(exp) printf(#exp);
       调用p(hello)的时候会将#exp换成”hello”

The # operator should not be confused with the null directive.

Use the # operator in a function-like macro definition according to the following rules:

  • A parameter following # operator in a function-like macro is converted into a character string literal containing the argument passed to the macro.
  • White-space characters that appear before or after the argument passed to the macro are deleted.
  • Multiple white-space characters imbedded within the argument passed to the macro are replaced by a single space character.
  • If the argument passed to the macro contains a string literal and if a \ (backslash) character appears within the literal, a second \ character is inserted before the original \ when the macro is expanded.
  • If the argument passed to the macro contains a " (double quotation mark) character, a \ character is inserted before the " when the macro is expanded.
  • The conversion of an argument into a string literal occurs before macro expansion on that argument.
  • If more than one ## operator or # operator appears in the replacement list of a macro definition, the order of evaluation of the operators is not defined.
  • If the result of the macro expansion is not a valid character string literal, the behavior is undefined.


Example: # operator

The following examples demonstrate the use of the # operator:

   #define STR(x)        #x
   #define XSTR(x)       STR(x)
   #define ONE           1

Invocation Result of Macro Expansion
STR(\n "\n" '\n') "\n \"\\n\" '\\n'"
STR(ONE) "ONE"
XSTR(ONE) "1"
XSTR("hello") "\"hello\""



2. ##:将前后两个的单词拼接在一起。例如《The C Programming Language》中的例子:
          #define cat(x,y) x##y
       调用cat(var, 123)展开后成为var123.
3. #@:将值序列变为一个字符
          #define ch(c)  #@c
       调用ch(a)展开后成为’a’.


ref:
http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/ilcrefer17.htm
http://www.trueeyu.com/?p=505

作者:翁志艺
分享到:
评论

相关推荐

    [c、c++]宏中#和##的用法(zz).docx

    c/c++ 宏中"#"和"##"的用法.

    浅析c++ 宏 #val 在unicode下的使用

    以下是对c++中宏#val在unicode下的使用方法进行了详细的分析介绍,需要的朋友可以参考下

    C/C++ 宏详解(详解)

    介绍了C/C++ 宏的用法和常用错误,众多C++书籍都忠告我们C语言宏是万恶之首,但事情总不如我们想象的那么坏,就如同goto一样。宏有 一个很大的作用,就是自动为我们产生代码。如果说模板可以为我们产生各种型别的...

    【zfind】简单的C/C++宏定义和类定义搜索工具

    zfind是本人写的一个简单的C/C++宏定义和类定义搜索工具,z是左的意思,即本人姓氏。取此名字是因为windows下有find命令,为避免冲突。另外本工具是本人在32位WIN7环境下使用的,其余环境本人不保证兼容。 写此工具...

    c++中#include lt;gt;与#include的区别详细解析

    所以像标准的头文件 stdio.h、stdlib.h等用这个方法。 而””首先在当前目录下寻找,如果找不到,再到系统目录中寻找。 这个用于include自定义的头文件,让系统优先使用当前目录中定义的。 然后是使用习惯的问题:...

    C++十六进制宏的用法详解

    流行的用法:用二进制的每一位代表一种状态。 001,010,100这样就表示三种状态。 通过或|运算就可以组合各种状态。 001|010=011 001|010|100=111 通过与&运算可以去除某种状态。 111&001=110 可以定义这样的宏组合成...

    #define 宏定义的一些用法总结

    C、C++语言中的“#define” 宏定义的一些用法总结,很好的资料

    C 宏 预编译 预处理

    C C++ 宏详解.doc C宏定义技巧.txt c语言预处理命令及其用法.doc C预处理.pdf C中的预编译宏定义.pdf ISO-ANSI+C标准译文与注解+C-C%2B%2B预处理部分.htm C++标准函数库.chm C函数速查.chm

    Visual C++ 实例精通

    示例描述:本章演示VC++6.0的基本使用方法。 01_HelloConsole 第一个使用VC++6.0开发的控制台应用程序。 02_HelloWindows 第一个使用VC++6.0开发的Windows应用程序。 03_MfcDialog 第一个使用VC++6.0开发的...

    C++宏,预处理器,RTTI,typeid与强制类型转换专题.pdf

    属于学习C++的附加内容,本文主要讲解了sizeof 操作符,预处理器,#define,RTTI 与typid 的作用与使用方法, 对于需要了解这方面内容的读者可以参考之。

    在C++中自定义宏的简单方法

    主要介绍了在C++中自定义宏的简单方法,作者建议使用类似定义函数一样的方法来定义宏,需要的朋友可以参考下

    C/C++语言宏定义使用实例详解

    主要介绍了 C/C++语言宏定义使用实例详解的相关资料,需要的朋友可以参考下

    由浅入深学C++基础进阶与必做300题 源程序

    串、指针与引用、使用函数、函数模板、错误和异常处理、宏和预编译、面 向对象的开发、封装、继承、多态、类模板、文件流、标准模板库STL和编 程实践等内容。 《由浅入深学C++--基础进阶与必做300题(附光盘)》涉及...

    C++ vector的用法小结

    c++ vector用法 C++内置的数组支持容器的机制,但是它不支持容器抽象的语义。要解决此问题我们自己实现这样的类。在标准C++中,用容器向量(vector)实现。容器向量也是一个类模板。 标准库vector类型使用需要的...

    C/C++中宏/Macro的深入讲解

    C/C++ 代码编译过程中,可通过相应参数来获取到各编译步骤中的产出,比如想看被预处理编译之后的宏,使用 gcc 使加上 -E 参数。 $ gcc -E macro.c 宏的定义 通过 #define 指令定义一个宏。 #define N

    C++中const用法总结.doc

    C++中const用法总结.doc C++中const用法总结.doc 1. const修饰普通变量和指针 2. const修饰函数参数 3. const 修饰函数返回值 4. const修饰类对象/对象指针/对象引用 5. const修饰成员变量 6. const修饰成员...

    Visual C++开发经验技巧宝典(第1章)

    0001 注释的使用方法及注意事项 2 0002 使用汇编语言 2 0003 如何使用内联函数 2 0004 如何使用#define自定义宏 3 0005 使用goto语句进行无条件跳转 3 0006 while循环转为for循环 3 0007 do while循环...

Global site tag (gtag.js) - Google Analytics