看《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的更多相关文章

  1. c/c++中#和##链接符号的用法

    #include <stdio.h> #include <stdlib.h> /* 英语原文: In function-like macros, a # operator be ...

  2. [2017.02.04] C++学习记录(1)

    编编程语言的目的是帮助程序员以代码的形式表述ideas.编程语言一方面为程序员提供一组关于可以做什么的抽象,另一方面为程序员提供可以被机器执行的轮子.C++编程语言,支持4种编程范式:过程式(Proc ...

  3. How does the compilation and linking process work?

    The compilation of a C++ program involves three steps: Preprocessing: the preprocessor takes a C++ s ...

  4. m4, autoconf

    http://www.gnu.org/software/m4/m4.html GNU M4 is an implementation of the traditional Unix macro pro ...

  5. C++ Core Guidelines

    C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...

  6. MARS3.6 Programming

    An Assembly Language I.D.E. To Engage Students Of All Levels * A Tutorial *2007 CCSC: Central Plains ...

  7. C语言中的预处理命令

    预处理功能是C语言的重要功能. 问:为什么要预处理,什么是预处理? 答:我们知道高级语言的运行过程是通过编译程序(编译器)把源代码翻译成机器语言,实现运行的.编译程序的工作包含:语法分析.词法分析.代 ...

  8. 一篇perfect描述“神秘”inf文件的文章[转]

    INF Files for Bears of Little BrainMay 1, 2003Brian Catlin Copyright � 2003 by Brian Catlin. All rig ...

  9. 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 ...

随机推荐

  1. 在Ubuntu上下载、编译和安装Android最新源码

    看完了前面说的几本书之后,对Linux Kernel和Android有一定的认识了,是不是心里蠢蠢欲动,想小试牛刀自己编译一把Android源码了呢?一直习惯使用Windows系统,而Android源 ...

  2. htaccess URL重写rewrite与重定向redirect(转)

    1. 将 .htm 页面映射到 .php 1 Options +FollowSymlinks 2 RewriteEngine on 3 RewriteRule ^(.*)\.htm$ $1.php [ ...

  3. TCP的封包与拆包

    对于基于TCP开发的通讯程序,有个很重要的问题需要解决,就是封包和拆包. 一.为什么基于TCP的通讯程序需要进行封包和拆包. TCP是个"流"协议,所谓流,就是没有界限的一串数据. ...

  4. Request 获取Url

    1.获取页面,HttpContext.Current.Request也是Request //获取当前页面url string myurl = HttpContext.Current.Request.U ...

  5. asp.net中调用COM组件发布IIS时常见错误 80070005解决方案

    很多人在.net开发Web站点中引用了COM组件,调试时一切正常,但一发布到IIS下就提示如下错误: 检索 COM 类工厂中 CLSID 为 {} 的组件时失败,原因是出现以下错误: 80070005 ...

  6. IIS支持APK/ISO文件下载的方法

    默认把安卓手机应用或游戏的apk格式文件上传到服务器空间是不能直接下载的,这是因为IIS的默认MIME类型中没有.apk文件,所以无法下载.@VCOO 既然.apk无法下载是因为没有MIME,那么添加 ...

  7. target,currentTarget,delegateTarget,srcElement

    第一种情况:就是IE9+和其他现代浏览器,支持addEventListener方法.其结果是: this总是等于currentTarget currentTarget总是事件监听者 target总是事 ...

  8. jrae源码解析(二)

    本文细述上文引出的RAECost和SoftmaxCost两个类. SoftmaxCost 我们已经知道,SoftmaxCost类在给定features和label的情况下(超参数给定),衡量给定权重( ...

  9. 重新开始学习javase_对象的摧毁

    一.概述(转:@深入理解Java虚拟机:JVM高级特性与最佳实践(最新第二版) ) 经过半个世纪的发展,内存的动态分配与内存回收技术已经相当成熟,一切看起来都进入了“自动化”时代,那为什么我们还要去了 ...

  10. hibernate_validator_09

    创建自己的约束规则 尽管Bean Validation API定义了一大堆标准的约束条件, 但是肯定还是有这些约束不能满足我们需求的时候, 在这种情况下, 你可以根据你的特定的校验需求来创建自己的约束 ...