怎么用宏定义连接字符串(类似strcat)的功能

ak47csu 2014-05-17 11:55:52
比如字符串A “hello", 字符串B为"world"
怎么用宏定义,实现A和B的连接,期望输出是"hello world"
...全文
1723 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-12-20
  • 打赏
  • 举报
回复
#与##在宏定义中的--宏展开
#include <stdio.h>
#define f(a,b) a##b
#define g(a)   #a
#define h(a) g(a)
int main()
{
        printf("%s\n", h(f(1,2)));   // 12
        printf("%s\n", g(f(1,2))); // f(1,2)
        return 0;
}
宏展开时:
如果宏定义以#开头,不展开参数,直接替换。
故g(f(1,2))--->#f(1,2)--->"f(1,2)";
如果宏定义不以#开头,展开参数,直接替换,由外层向里层,如果碰到的是#开头的宏,不继续往里层展开,往外层展开。
由外层向里层,如果碰到的是以非#开头的宏,继续往里层走,直至最里层,开始一层层往外层展开。
故h(f(1,2))--->h(12)--->g(12)---->#12----->"12"。
PS:
##在宏中定义,是字符连接符
如a##b##c 等同于 "abc"
#在宏开头出现,是表示宏展开的方式不同
#a 等同于"a"
#abc 等同于 "abc"
复杂的:
#include <stdio.h>
#define f(a,b) a##b
#define g(a)   #a
#define h(a) g(a)
int main()
{
        char a = 'a';
        cout<<g(a)<<endl; // a
        cout<<g(g(a))<<endl; // a
        printf("%s\n", h(f(1,2)));   // 12
        printf("%s\n", g(f(1,2))); // f(1,2)
        printf("%s\n", g(h(f(1,2)))); // h(f(1,2))
        printf("%s\n", h(g(f(1,2)))); // "f(1,2)"
        printf("%s\n", h(h(f(1,2)))); // "12"
        system("pause");
        return 0;
}
预处理后的:(在编译选项中添加/EP /P后编译生成的.i文件。gcc加-E)
int main()
{
        char a = 'a';
        cout<<"a"<<endl;
        cout<<"g(a)"<<endl;
        printf("%s\n", "12");
        printf("%s\n", "f(1,2)");
        printf("%s\n", "h(f(1,2))");
        printf("%s\n", "\"f(1,2)\"");
        printf("%s\n", "\"12\"");
        system("pause");
        return 0;
}
---------------------------------------------------
宏解析
1.       ##操作符
##操作符它的作用是在替代表中将其前后的参数连接成为一个预处理符号,它不能出现于宏替代表的开端和末尾。
例:
#define concat(s,t) s##t
#define AAA ABC
concat(A, AA)
将被替换成
ABC
2.       重新扫描和替换
在替换列表中的所有参数替换过之后,预处理器将对结果token序列重新扫描以便对其中的宏再次替换。
当正在替换的宏在其替换列表中发现自身时,就不再对其进行替换。在任何正在嵌套替换的宏的替换过程中遇到正被替换的宏就对其不再进行替换(防止递归)。
例:
#define ROOT AAA CCC
#define AAA ROOT
ROOT
将被替换成
ROOT CCC
赵4老师 2016-12-20
  • 打赏
  • 举报
回复
C++ String Literals A string literal consists of zero or more characters from the source character set surrounded by double quotation marks ("). A string literal represents a sequence of characters that, taken together, form a null-terminated string. Syntax string-literal : "s-char-sequenceopt" L"s-char-sequenceopt" s-char-sequence : s-char s-char-sequence s-char s-char : any member of the source character set except the double quotation mark ("), backslash (\), or newline character escape-sequence C++ strings have these types: Array of char[n], where n is the length of the string (in characters) plus 1 for the terminating '\0' that marks the end of the string Array of wchar_t, for wide-character strings The result of modifying a string constant is undefined. For example: char *szStr = "1234"; szStr[2] = 'A'; // Results undefined Microsoft Specific In some cases, identical string literals can be “pooled” to save space in the executable file. In string-literal pooling, the compiler causes all references to a particular string literal to point to the same location in memory, instead of having each reference point to a separate instance of the string literal. The/Gf compiler option enables string pooling. END Microsoft Specific When specifying string literals, adjacent strings are concatenated. Therefore, this declaration: char szStr[] = "12" "34"; is identical to this declaration: char szStr[] = "1234"; This concatenation of adjacent strings makes it easy to specify long strings across multiple lines: cout << "Four score and seven years " "ago, our forefathers brought forth " "upon this continent a new nation."; In the preceding example, the entire string Four score and seven years ago, our forefathers brought forth upon this continent a new nation. is spliced together. This string can also be specified using line splicing as follows: cout << "Four score and seven years \ ago, our forefathers brought forth \ upon this continent a new nation."; After all adjacent strings in the constant have been concatenated, the NULL character, '\0', is appended to provide an end-of-string marker for C string-handling functions. When the first string contains an escape character, string concatenation can yield surprising results. Consider the following two declarations: char szStr1[] = "\01" "23"; char szStr2[] = "\0123"; Although it is natural to assume that szStr1 and szStr2 contain the same values, the values they actually contain are shown in Figure 1.1. Figure 1.1 Escapes and String Concatenation Microsoft Specific The maximum length of a string literal is approximately 2,048 bytes. This limit applies to strings of type char[] and wchar_t[]. If a string literal consists of parts enclosed in double quotation marks, the preprocessor concatenates the parts into a single string, and for each line concatenated, it adds an extra byte to the total number of bytes. For example, suppose a string consists of 40 lines with 50 characters per line (2,000 characters), and one line with 7 characters, and each line is surrounded by double quotation marks. This adds up to 2,007 bytes plus one byte for the terminating null character, for a total of 2,008 bytes. On concatenation, an extra character is added to the total number of bytes for each of the first 40 lines. This makes a total of 2,048 bytes. (The extra characters are not actually written to the string.) Note, however, that if line continuations (\) are used instead of double quotation marks, the preprocessor does not add an extra character for each line. END Microsoft Specific Determine the size of string objects by counting the number of characters and adding 1 for the terminating '\0' or 2 for type wchar_t. Because the double quotation mark (") encloses strings, use the escape sequence (\") to represent enclosed double quotation marks. The single quotation mark (') can be represented without an escape sequence. The backslash character (\) is a line-continuation character when placed at the end of a line. If you want a backslash character to appear within a string, you must type two backslashes (\\). (SeePhases of Translation in the Preprocessor Reference for more information about line continuation.) To specify a string of type wide-character (wchar_t[]), precede the opening double quotation mark with the character L. For example: wchar_t wszStr[] = L"1a1g"; All normal escape codes listed in Character Constants are valid in string constants. For example: cout << "First line\nSecond line"; cout << "Error! Take corrective action\a"; Because the escape code terminates at the first character that is not a hexadecimal digit, specification of string constants with embedded hexadecimal escape codes can cause unexpected results. The following example is intended to create a string literal containing ASCII 5, followed by the characters five: \x05five" The actual result is a hexadecimal 5F, which is the ASCII code for an underscore, followed by the characters ive. The following example produces the desired results: "\005five" // Use octal constant. "\x05" "five" // Use string splicing.
bluesky20080510 2016-12-20
  • 打赏
  • 举报
回复
知识点: 1. #x可以将x字符串化; 2. 几个字符串放宏定义里面,会自动合并为一个字符串。 比如#define teststr "hello ""world", 使用printf时可以输出"hello world"
bluesky20080510 2016-12-20
  • 打赏
  • 举报
回复
图片怎么不显示了? 给出代码吧: #include <stdio.h> #define cat(x,y) #x" "#y int main() { printf("%s\n", cat(hello, world)); return 0; }
bluesky20080510 2016-12-20
  • 打赏
  • 举报
回复
见图片,如果要增加空格,可以修改
#define cat(x,y) #x" "#y
mujiok2003 2014-05-18
  • 打赏
  • 举报
回复
自寻烦恼:-)
FightForProgrammer 2014-05-18
  • 打赏
  • 举报
回复
简单问题复杂化?这个貌似不是宏的最主要的用途.
YinQingwei1986 2014-05-18
  • 打赏
  • 举报
回复
#define CAT(x,y) #(x##y) #是为#后首个参数加"" ##是连接前后 你试下
xiaoqiao_82 2014-05-18
  • 打赏
  • 举报
回复
引用 3 楼 taodm 的回复:
字符串直接写一起就行了,哪需要宏这么复杂的东西。 "Hello" "world"
赞同。
taodm 2014-05-18
  • 打赏
  • 举报
回复
字符串直接写一起就行了,哪需要宏这么复杂的东西。 "Hello" "world"
wang0635 2014-05-18
  • 打赏
  • 举报
回复

#define	cat(x, y) x " " y

printf("%s\n", cat("Hello", "world"));

bug_________ 2014-05-18
  • 打赏
  • 举报
回复
引用define 只会原样输出的 如果只要输出"hello world" 可以define的时候适当的调整引号空格
bobo_包子 2014-05-18
  • 打赏
  • 举报
回复
引用 5 楼 YinQingwei1986 的回复:
#define CAT(x,y) #(x##y) #是为#后首个参数加"" ##是连接前后 你试下
就这个

69,740

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧