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 ...
随机推荐
- Gridview实现银行选择列表
[MainActivity.java] package com.example.activitydemo; import android.os.Bundle; import android.view. ...
- Tips--怎么使用谷歌搜索
修改hosts即可: hosts在哪? windows下:C:\Windows\System32\drivers\etc 管理员身份打开,并将下载好的hosts文件内容,添加到原有的hosts文件末尾 ...
- BestCoder 1st Anniversary
Souvenir Accepts: 1078 Submissions: 2366 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 26 ...
- 5阻止A默认行为和JS实现页面跳转的方法
<!--HTML中阻止A标签的默认行为: href="javascript:;" href="javascript:void 0;"--><! ...
- Linq101-Partitioning
using System; using System.Linq; namespace Linq101 { class Partitioning { /// <summary> /// Th ...
- Struts2 删除后直接直接到List显示页面
package com.sun; import java.util.List; import java.util.Map; import org.hibernate.Session; import o ...
- JavaScript省市联动
<html> <head> <title>JS省市二级联动菜单,整理收集.</title> </head> <body bgcolor ...
- C文件函数总结
1.fopen(打开文件) 表头文件 #include<stdio.h> 定义函数 FILE *fopen(const char * path,const char * mode); pa ...
- [LeetCode OJ] Max Points on a Line—Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
//定义二维平面上的点struct Point { int x; int y; Point(, ):x(a),y(b){} }; bool operator==(const Point& le ...
- windows 7下安装python+mongodb
1. python安装 下载:http://python.org/download/ 直接双击安装,安装完后将路径加入系统环境变量path中. 2. mongodb安装 下载:http://www.m ...