The double-number-sign or "token-pasting" operator (##), which is sometimes called the "merging" operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token and therefore cannot be the first or last token in the macro definition.
If a formal parameter in a macro definition is preceded or followed by the token-pasting operator, the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed on the argument prior to replacement.
Then, each occurrence of the token-pasting operator in token-string is removed, and the tokens preceding and following it are concatenated. The resulting token must be a valid token. If it is, the token is scanned for possible replacement if it represents a macro name. The identifier represents the name by which the concatenated tokens will be known in the program before replacement. Each token represents a token defined elsewhere, either within the program or on the compiler
command line. White space preceding or following the operator is optional.
This example illustrates use of both the stringizing and token-pasting operators in specifying program output:
#define paster( n ) printf( "token" #n " = %d", token##n )
int token9 = 9;
If a macro is called with a numeric argument like
paster( 9 );
the macro yields
printf( "token" "9" " = %d", token9 );
which becomes
printf( "token9 = %d", token9 );
The number-sign or "stringizing" operator (#) converts macro parameters (after expansion) to string constants. It is used only with macros that take arguments. If it precedes a formal parameter in the macro definition, the actual argument passed by the macro invocation is enclosed in quotation marks and treated as a string literal. The string literal then replaces each occurrence of a combination of the stringizing operator and formal parameter within the macro definition.
White space preceding the first token of the actual argument and following the last token of the actual argument is ignored. Any white space between the tokens in the actual argument is reduced to a single white space in the resulting string literal. Thus, if a comment occurs between two tokens in the actual argument, it is reduced to a single white space. The resulting string literal is automatically concatenated with any adjacent string literals from which it is separated only by white space.
Further, if a character contained in the argument usually requires an escape sequence when used in a string literal (for example, the quotation mark (") or backslash (\)character), the necessary escape backslash is automatically inserted before the character. The following example shows a macro definition that includes the stringizing operator and a main function that invokes the macro:
#define stringer( x ) printf( #x "\n" )
int main()
{
stringer( In quotes in the printf function call\n );
stringer( "In quotes when printed to the screen"\n );
stringer( "This: \" prints an escaped double quote" );
}
Such invocations would be expanded during preprocessing, producing the following code:
int main()
{
printf( "In quotes in the printf function call\n" "\n" );
printf( "\"In quotes when printed to the screen\"\n" "\n" );
printf( "\"This: \\\" prints an escaped double quote\"" "\n" );
}
When the program is run, screen output for each line i
s as follows:
In quotes in the printf function call
"In quotes when printed to the screen"
"This: \" prints an escaped double quotation mark"
Microsoft Specific
The Microsoft C (versions 6.0 and earlier) extension to the ANSI C standard that previously expanded macro formal arguments appearing inside string literals and character constants is no longer supported. Code that relied on this extension should be rewritten using the stringizing (#) operator.
END Microsoft Specific
分享到:
相关推荐
"#ifdef,#else,#endif和#ifndef的用法" #ifdef、#else、#endif 和#ifndef 是 C/C++ 预处理命令中条件编译的四个基本指令,它们可以帮助开发者在编译前对代码进行选择性编译,从而实现代码的灵活使用和高效编译。 #...
下面我们将详细介绍#与##的用法和示例。 一、#基本用法 #运算符的基本用法是将参数转换为字符串。在宏定义中,#运算符将参数转换为一个字符串,例如: #define dprint(expr) printf(#expr " = %d\n", expr) 当...
C 语言宏中 "#" 和 "##" 的用法是非常重要的,特别是在查看 Linux 内核源码的过程中,这两种符号经常被使用。下面我们将从一般用法、宏参数展开、应用特例三个方面来详细讲解这两种符号的用法。 一、一般用法 在 C...
C语言中宏定义"#"和"##"符号的用法 在C语言中,宏定义是一种非常有用的机制,可以帮助开发者简化代码、提高效率和可读性。其中,"#"和"##"符号是两个非常重要的宏符号,它们可以帮助开发者在宏定义中实现字符串化和...
在C语言中,“#”和“##”是预处理指令中使用的特殊操作符,它们在宏定义和对象参数化的上下文中具有独特的用法。以下是关于这两个操作符的详细知识点: 1. 宏定义中的“#”操作符 “#”操作符在宏定义中用于将宏...
在本文中,我们将详细介绍 #define 指令的用法、实例和注意事项。 定义 ---- #define 指令的基本语法有两种形式: ```c #define identifier token-stringopt #define identifier[( identifieropt, ... , ...
下面我们将详细地介绍它们的用法和实例分析。 #ifdef 命令 #ifdef 命令是用来判断一个宏是否已经被定义,如果宏已经被定义,那么编译器将编译后面的代码,否则将跳过。 例如: ``` #define NUM #ifdef NUM ...
本文将详细介绍`#`符号的使用方法及其应用场景,帮助读者更好地理解和掌握Struts2框架的相关知识。 #### 一、Struts2简介 Struts2是基于Struts1的基础上发展起来的一款开源Web应用框架,它使用MVC(Model-View-...
#import用法详细分析 #import指令是VC中的一种编译环境语句,它允许开发者将COM组件导入到C++程序中,以便于使用COM接口。下面是对#import指令的详细分析。 一、基本语法 #import指令的基本语法如下: ``` #...
本文将详细介绍Delphi 中 IdTcpServer 和 IdTcpClient 的使用方法,并通过实例演示如何构建一个简单的客户端与服务端应用程序。 #### 二、IdTcpServer 介绍及使用 ##### 1. 引入组件 要使用 IdTcpServer 组件,...
#ifdef_#else_#endif_#if_#ifndef的用法.txt
### Selenium2 Java版 使用方法详解 #### 一、引言 随着互联网技术的快速发展,Web应用变得日益复杂,为了确保产品的质量和用户体验,自动化测试成为了一种趋势和必要手段。Selenium作为一款广受欢迎的自动化测试...
"TIA博途中上升沿的使用方法" 在Totally Integrated Automation ...TIA博途中上升沿的使用方法多种多样,选择合适的方法取决于实际应用场景和控制逻辑的要求。正确使用上升沿可以提高自动化控制系统的可靠性和实时性。
无论是在.NET 1.1还是.NET 2.0环境下,都可以通过调用解析方法来将原始的HL7消息转换为.NET中的对象模型,方便后续的处理和分析。 ##### .NET 1.1 在.NET 1.1环境下,可以通过`ca.uhn.hl7v2.parser.PipeParser`来...
这些特殊用法主要用于字符串化和连接操作,但在实际编程中较少见。 #### 3.2 `#define` 的多行定义 `#define` 还可以用于定义多行的代码块,这对于复杂的宏定义非常有用。为了实现这一点,需要在每行的末尾添加反...
### nmon 使用方法详解:监控Linux服务器资源与性能 #### 引言 在现代IT环境中,服务器性能监控是确保系统稳定性和优化资源分配的关键。nmon作为一种轻量级且功能强大的监控工具,被广泛用于AIX和Linux操作系统上...
### CFileDialog的用法与简介 #### 一、概述 `CFileDialog` 是 MFC (Microsoft Foundation Classes) 提供的一个用于文件选择对话框的...掌握 `CFileDialog` 的使用方法对于进行基于 MFC 的文件操作开发来说至关重要。
### GATK使用方法详细介绍 #### 一、GATK概览及预备知识 ##### (1) GATK适用范围及版本更新 - **适用范围**:GATK主要用于处理人类全基因组和外显子组测序数据,且特别针对illumina数据格式。虽然目前尚未支持...
从给定的文件信息来看,标题为“数字万用表的使用方法详细图解”,描述指出这是一份适合初学者的详细指南,包含了图表,标签则聚焦于“万用表使用”。这部分内容虽然包含了大量非识别字符,但通过标题、描述和标签,...
本文旨在基于站长多年的实践经验,全面总结FSO组件的使用方法,为用户提供一个详尽的技术指南。尽管无法覆盖FSO的所有细节,但本文将聚焦于其基本使用、核心方法和实用案例,以满足日常开发需求。 #### 二、FSO组件...