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 ...
随机推荐
- Hibernate实体对象三种状态
Hibernate实体对象生命周期: 1. 自由状态(Transient,临时状态,瞬态) 在内存中自由存在,与数据库无关,未被Hibernate的Session管理 2. 持久状态(Persiste ...
- SPOJ 3937 - Wooden Sticks 最长上升子序列LIS
给了n个(n<=5000)木棍的长度hi与宽度wi(均小于10000),现在机器要打磨这些木棍,如果相邻连个木棍hi<=hj并且wi<=wj就不需要调整机器,问如何排序使得机器调整的 ...
- 将JSON数组显示前台Table中
将JSON数组显示前台Table中 在最近的学习中遇到一个小问题,如何把JSON数组显示在前台的table中,经过一番借鉴和学习之后终于解决了这个问题.具体的代码如下: (前提:利用ajax获取过来的 ...
- Canvas transform浅析
没有前奏,直接进入主题 transform调用方法: ctx.transform(a,b,c,d,e,f);如下 var ctx = document.getElementById("myC ...
- Ext4.1 Grid 分页查询
转载:http://blog.csdn.net/zyujie/article/details/16362747 最近用Ext4.1自己做了做项目的练习:把一些知识点,在这里记录下来了! 上面一个for ...
- 简单实现图片间的切换动画 主要用到ViewPager
简单实现图片间的切换动画 主要用到ViewPagerViewPager是android扩展包v4包中的类,这个类可以让用户左右切换当前的view.ViewPager类需要一个PagerAdapter适 ...
- MySQL where
MySQL where 子句 我们知道从MySQL表中使用SQL SELECT 语句来读取数据. 如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句中. 语法 以下是SQL ...
- Mysql 源码编译教程贴
题外话:这是一篇教程贴,不仅学的是mysql的编译,还是一些编译的知识.我也是一个菜鸟,写一些感悟和心得,有什么问题可以批评指正,谢谢! 如果只是为了安装请移到我的另一篇安装贴: Mysql安装贴 环 ...
- QComboBox实现复选功能
需求: 下拉列表有复选功能 不可编辑 显示所有选中项 关于QComboBox的复选功能有几种方案: QStandardItemModel + QStandardItem QListWidget + ...
- spring 入门笔记(一)
最近学习spring 通过笔记形式加深自己对spring的理解,也希望能跟各位入门者分享和讨论. 一.下载spring 下载spring也费了不少功夫,目前还没从spring官网找到下载入口,我从下面 ...