c/c++中#和##链接符号的用法
#include <stdio.h>
#include <stdlib.h>
/*
英语原文:
In function-like macros, a # operator before an identifier in the replacement-list runs
the identifier through parameter replacement and encloses the result in quotes,
effectively creating a string literal. In addition, the preprocessor adds backslashes to escape
the quotes surrounding embedded string literals, if any, and doubles the backslashes within
the string as necessary. All leading and trailing whitespace is removed,
and any sequence of whitespace in the middle of the text (but not inside embedded string literals)
is collapsed to a single space. This operation is called "stringification".
If the result of stringification is not a valid string literal, the behavior is undefined.
*/
/*翻译
在类函数宏中,替换参数标示符前加#,展开结果实际上是替换参数加双引号的字符串.
另外,如果参数中有双引号,预处理器替换后会在双引号前加反斜杠\,转义字符。
如果有反斜杠,对反斜杠加反斜杠转义。
前置后置空白字符删除,中间连续多个空白字符压缩成一个(字符串除外)。
这样的操作被称为"字符串化"。假如结果不是有效字符串化,行为没有被定义。
*/
/*结论:
1.#在宏替换中产生"参数"这样的字符串
2.参数中"被展开成\"
3.参数中\被展开成\\
4.参数前置后置空白字符展开时被删除
5.参数中间的空白序列展开时变成一个,参数为双引号引起来的字符串除外
6.展开后是无效字符串,行为没有被定义,不同的预处理器有不同的处理方式
*/
#define FUNCTION_LIKE_MACRO(params) (#params)
/*
英语原文:
A ## operator between any two successive identifiers in the replacement-list runs parameter replacement
on the two identifiers and then concatenates the result. This operation is called "concatenation" or "token pasting".
Only tokens that form a valid token together may be pasted: identifiers that form a longer identifier,
digits that form a number, or operators + and = that form a +=.
A comment cannot be created by pasting / and * because comments are removed from text
before macro substitution is considered. If the result of concatenation is not a valid token, the behavior is undefined.
*/
/*
翻译:
##操作符在两个连续的标示符之间,当宏代替时,结果链接这两个标示符.
这种操作被称作"链接"或者"符号黏贴".
只有能形成有效的标示符的符号才能黏贴在一起:标示符形成一个长标示符;数字形成数,+和=形成+=.
/和*不能形成注释,因为预处理器在宏代替之前都有会把注释去掉。
假如链接不是有效的符号,这种行为没有定义。
*/
/*
1.##链接标示符形成一个标示符
2.只有形成有效的标示符才能链接
3.链接结果是无效符号怎样验证
*/
#define CONCATE_NUMBER(param) (100##param)
#define CONCATE_ADDEQU(add,equ) add##equ
#define CONCATE_FUNC(name) FUNC_##name //打印函数名字,使用##举例
void CONCATE_FUNC(myfun)(int var)
{
printf("%s[%d]var=%d\n",__FUNCTION__,__LINE__,var);
} int main()
{
unsigned int var;
//# usage=========
//参数展开为字符串
printf("str[%s]\n",FUNCTION_LIKE_MACRO(
printf("str[hello!]\n");//这两条语句结果一样 //展开后"被转义\"
printf("str[%s]\n",FUNCTION_LIKE_MACRO("hello!"));
printf("str[\"hello!\"]\n"); //这个和英语原文描述的不一样,我理解错了吗?
printf("str[%s]\n",FUNCTION_LIKE_MACRO(dir1\\sub0));
printf("str[dir1\\sub0]\n"); //前后空白字符删除
printf("str[%s]\n",FUNCTION_LIKE_MACRO( hello ));
printf("str[hello]\n"); //中间件空白字符序列压缩成一个,字符串内除外
printf("str[%s]\n",FUNCTION_LIKE_MACRO(hello world "good morning"));
printf("str[hello world \"good morning\"]\n"); //无效字符串,这个不明白,怎样验证
printf("str[%s]\n",FUNCTION_LIKE_MACRO(\a));
printf("str[\a]\n"); //##uase
printf("concate number(123)=%d\n",CONCATE_NUMBER(123)); var=11111;
// var += 22222;
var CONCATE_ADDEQU(+,=) 22222;
printf("var=%d\n",var); CONCATE_FUNC(myfun)(6890); return 0;
}

c/c++中#和##链接符号的用法的更多相关文章
- OGNL中的#、%和$符号的用法
转自:https://blog.csdn.net/qq_24963197/article/details/51773224 一.OGNL中的#.%和$符号 1.#符号的三种用法 1)访问非根对象属性, ...
- 嵌入式C语言自我修养 09:链接过程中的强符号和弱符号
9.1 属性声明:weak GNU C 通过 __atttribute__ 声明weak属性,可以将一个强符号转换为弱符号. 使用方法如下. void __attribute__((weak)) fu ...
- GNU C/C++ __attributes__ GCC中的弱符号与强符号
最近在看一些源代码,遇到了一些使用__attribute__修饰函数和变量的属性方面的代码,不是太了解,很是汗颜,再此做个总结: GCC使用__attribute__关键字来描述函数,变量和数据类 ...
- Ruby Rails学习中:Sass 和 Asset Pipeline,布局中的链接(Rails路由,具名路由),用户注册: 第一步
接上篇: 一.Sass 和 Asset Pipeline Rails 中最有用的功能之一是 Asset Pipeline, 它极大地简化了静态资源文件(CSS.JavaScript 和图像)的生成和管 ...
- html中的alt和title用法区别
html中的alt和title用法区别 首先明确一下概念,alt是html标签的属性,而title既是html标签,又是html属性.title标签这个不用多说,网页的标题就是写在<title& ...
- [CSAPP-II] 链接[符号解析和重定位] 静态链接 动态链接 动态链接接口
1 平台 转http://blog.csdn.net/misskissc/article/details/43063419 1.1 硬件 Table 1. 硬件(lscpu) Architecture ...
- C#中Config文件中,特殊符号的书写方法。
App.config: 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration& ...
- 实例讲解Linux系统中硬链接与软链接的创建
导读 Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接.硬链接与软链接的区别从根本上要从Inode节点说 ...
- 关于C语言中的强符号、弱符号、强引用和弱引用的一些陋见,欢迎指正
首先我表示很悲剧,在看<程序员的自我修养--链接.装载与库>之前我竟不知道C有强符号.弱符号.强引用和弱引用.在看到3.5.5节弱符号和强符号时,我感觉有些困惑,所以写下此篇,希望能和同样 ...
随机推荐
- Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- node 学习笔记 - path 处理
本文同步自我的个人博客:http://www.52cik.com/2015/12/04/learn-node-path.html path 模块是 node 用于整理.转换.合并路径的神器,只要是路径 ...
- MVC+EF 理解和实现仓储模式和工作单元模式
MVC+EF 理解和实现仓储模式和工作单元模式 原文:Understanding Repository and Unit of Work Pattern and Implementing Generi ...
- 编程中的offsetof
linux和windows平台都已经定义了offsetof函数,用于取struct类型中某个变量的偏移量 在stddef.h头文件中,该宏的完整说明如下: #ifdef __cplusplus #if ...
- Oracle基础语法
--表create table tb_myTable( mname vardhar2(30), pwd varchar2(30)); --存储过程create or replace procedure ...
- 硬件升级win8.1重新安装系统
上次重装系统一年后升级了硬件配置,双11后,再次折腾系统. 配置硬件的选择 部分配件已经升级过了,之前的一直是AMD平台,发热大功耗高,08年我配的AMD 8450+映泰GX790 128M待机在10 ...
- Spark源码在Eclipse中部署/编译/运行
(1)下载Spark源码 到官方网站下载:Openfire.Spark.Smack,其中Spark只能使用SVN下载,源码的文件夹分别对应Openfire.Spark和Smack. 直接下载Openf ...
- nios II--实验3——led 100M软件部分
软件开发 参照实验二(led),该实验与实验二(led)的不同之处在于系统的时钟由50M提成为100M.运行结果,在调试窗口输出Hello from Nios II!,并且板上的四个LED灯流动显示, ...
- Socket网络编程--FTP客户端
Socket网络编程--FTP客户端(1)(Windows) 已经好久没有写过博客进行分享了.具体原因,在以后说. 这几天在了解FTP协议,准备任务是写一个FTP客户端程序.直接上干货了. 0.了解F ...
- 调用天气Api实现天气查询
上面是简单截图: 前台代码: @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name=&qu ...