Macro Substitution
看《C程序设计语言》(英文版)学到的两个用法。
两个很简单的宏用法。
#的用法: if, however, a parameter name is preceded by a # in the replacement text, the combination will be expanded into a quoted string with the parameter replaced by the actual argument.
#include <stdlib.h>
#define dprint(expr) printf(#expr " = %g\n", expr);
int main() {
double x = 1.0, y = 2.0;
dprint(x/y);
return ;
}
结果为 x/y = 0.5
#define dprint(expr) printf(#expr " = %g\n", expr);
When this is invoked, as in
#define dprint(expr) printf(#expr " = %g\n", expr);
the macro is expanded into
printf("x/y" " = %g\n", x/y);
and the strings are concatenated, so the effect is
printf("x/y = %g\n", x/y);
Within the actual argument, each " is replaced by \" and each \ by \\, so the result is a legal string constant.
##的用法: The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion. If a paramter in the replacement text is a adjacent to a ##, the parameter is replaced by the actual argument, the ## and surrouding white space are removed, and the result is re-scanned.
例如:
#include <stdio.h>
#include <stdlib.h> #define paste(front, back) front ## back int main() {
int x, x1;
x = , x1 = ; printf("%d\n", paste(x, )); return ;
}
输出结果是3.
#define paste(front, back) front ## back
so paste(x, 1) creates the token x1.
Macro Substitution的更多相关文章
- c/c++中#和##链接符号的用法
#include <stdio.h> #include <stdlib.h> /* 英语原文: In function-like macros, a # operator be ...
- [2017.02.04] C++学习记录(1)
编编程语言的目的是帮助程序员以代码的形式表述ideas.编程语言一方面为程序员提供一组关于可以做什么的抽象,另一方面为程序员提供可以被机器执行的轮子.C++编程语言,支持4种编程范式:过程式(Proc ...
- How does the compilation and linking process work?
The compilation of a C++ program involves three steps: Preprocessing: the preprocessor takes a C++ s ...
- m4, autoconf
http://www.gnu.org/software/m4/m4.html GNU M4 is an implementation of the traditional Unix macro pro ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
- MARS3.6 Programming
An Assembly Language I.D.E. To Engage Students Of All Levels * A Tutorial *2007 CCSC: Central Plains ...
- C语言中的预处理命令
预处理功能是C语言的重要功能. 问:为什么要预处理,什么是预处理? 答:我们知道高级语言的运行过程是通过编译程序(编译器)把源代码翻译成机器语言,实现运行的.编译程序的工作包含:语法分析.词法分析.代 ...
- 一篇perfect描述“神秘”inf文件的文章[转]
INF Files for Bears of Little BrainMay 1, 2003Brian Catlin Copyright � 2003 by Brian Catlin. All rig ...
- The difference between macro and function I/Ofunction comparision(from c and pointer )
macro is typeless and execute faster than funtion ,becaus of the overhead of calling and returnning ...
随机推荐
- ZOJ 1301 The New Villa (BFS + 状态压缩)
题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=10),有一天晚上他回家时发现所有的灯(除了他出发的房间)都是关闭的,而他想回卧室去休息 ...
- mysql源码分析
http://blog.csdn.net/u012935160/article/category/2697485
- ab ApacheBench web测试工具
http://www.ibm.com/developerworks/cn/linux/l-cn-ulimit/ ApacheBench参数说明 格式:ab [options] [http://]hos ...
- Android手机适配——UI图片适配
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/50727753 在Android项目当中,drawable文件夹都是用来放置图片资源 ...
- type和create type
type和create type 异同点: create type 可在库中生成一个长期有效的自定义类型对象,而type作用域仅限于语句块中: 两者都可以自定义数据类型: 各种ty ...
- 【转】 NSArray copy 问题
转自: http://blog.sina.com.cn/s/blog_6b1e4a060102uz0i.html 好久没写博客了,今天看到同事的代码中用到了 copy 这个 方法,之前也有了解 ...
- ios 中的UI控件学习总结(1)
UIKit框架提供了非常多功能强大又易用的UI控件 下面列举一些在开发中可能用得上的UI控件 UIButton 按钮 UILabel 文本标签 UITextField 文本输入框 UIImageVie ...
- C++对象数组操作误区
由于语义上的需要导致语法的上缺陷,所以导致对象数组在C++中存在陷阱. C++语境:一个基类指针或引用是可以指向派生类对象的,以此可来表现C++对运行时多态的需求: 创建一个对象数组将返回首元素的首地 ...
- RedHat9上安装jdk
1.先在windows下载jdk:jdk-6-dlj-linux-i586.bin 2.用ftp上传给linux下 3.chmod 777 jdk-6-dlj-linux-i586.bin 4.将jd ...
- table表格cellspacing与cellpadding属性
cellspacing属性 用来指定表格各单元格之间的空隙. cellpadding属性 用来指定单元格内容与单元格边界之间的空白距离的大小. 此属性的参数值也是数字,表示单元格内容与上下边界之间空白 ...