看《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. Solaris下怎样改动文件创建时间及查询

    Solaris下怎样改动文件创建时间及查询 实验演示: 1.核对时间 [root@S1011:/]# date Tue Jul 15 21:37:01 CDT 2014 --若时间不对请先按例如以下格 ...

  2. Windows Minifilter驱动 - 调式 (4)

    写不论什么程序动态调试是很重要的.驱动开发也不例外. 通常如今写驱动的时候,都是在VM上跑的,调试手段基本也是本地windbg + 虚拟机. 虚拟机配置 我用的是win7, 第一步,看以下.成功运行后 ...

  3. raknet unity3d

    Raknet是一高性能的跨平台的网络库. 他主要基于UDP实现,性能非常好,能够做server. 鉴于unity3d较差的网络支持. 本人成功实现了raknet c# for unity3d的使用,s ...

  4. Liunx Shell入门

    本人也是初学习Liunx,如有错误请指出.Liunx版本:Ubuntu 14.04 一.Liunx命令基础 在Ubuntu下打开终端快捷键为:ctrl+Alt+T Liunx命令的基本格式:comma ...

  5. Android MediaCodec 使用例子

    Android MediaCodec 使用例子 下面的例子是使用MediaCodec 录制到文件的例子. 1 public class AvcEncoder { private MediaCodec ...

  6. 初识Ajax技术

    Ajax:(Asynchronous JavaScript And Xml)是一种整合了JavaScript.XML.CSS等现有技术 Ajax工作流程:   纯javaScript的Ajax请求   ...

  7. 从零开始,在windows上用nodejs搭建一个静态文件服务器

    从零开始,在windows上用nodejs搭建一个静态文件服务器 首先安装nodejs: 新建一个node文件夹 下载node.exe到该文件夹 下载npm然后解压到该文件夹 现在node文件夹是这样 ...

  8. 关于php读mysql数据库时出现乱码的解决方法

    关于php读mysql数据库时出现乱码的解决方法 php读mysql时,有以下几个地方涉及到了字符集. 1.建立数据库表时指定数据库表的字符集.例如 create table tablename ( ...

  9. HTTP 状态响应码 意思详解/大全

    HTTP 状态响应码 意思详解/大全 转:http://blog.csdn.net/helin916012530/article/details/29842595 HTTP状态码(HTTP Statu ...

  10. mysql的limit经典用法及优化

    用法一   SELECT `keyword_rank`.* FROM `keyword_rank` WHERE (advertiserid='59') LIMIT 2 OFFSET 1;   比如这个 ...