预处理程序指令字符串操作

时间:2013-11-04 13:49:27

标签: c-preprocessor

#define PRINT() printf("STRING:"); printf("%s %s",CONV_STR(20),CONV_STR(NAME));    
#define CONV_STR(X) #X    
#define NAME India    
int main()    
{
PRINT();
getch();
return 0;
}

为什么PRINT中的第一个printf没有被执行?

1 个答案:

答案 0 :(得分:1)

对我而言,它按预期工作。我稍微更改了代码:插入#include<stdio.h>,删除getchr并添加printf("\n")。两个print都将被执行。我使用编译器的-E选项来创建具有扩展宏的文件。

$ uname -a
Linux xxxxxxxx.xxx.xx 2.6.18-238.9.1.el5 #1 SMP Tue Apr 12 18:10:56 EDT 2011 i686 athlon i386 GNU/Linux
$ gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-50)
$ cat prog.c
#include 
#define PRINT() printf("STRING:"); printf("%s %s",CONV_STR(20),CONV_STR(NAME));
#define CONV_STR(X) #X
#define NAME India
int main()
{
PRINT();
printf("\n");
return 0;
}
$ gcc  -o prog   prog.c
$ ./prog
STRING:20 NAME
$ gcc -E  -o prog.txt   prog.c
$ tail prog.txt
# 2 "prog.c" 2



int main()
{
printf("STRING:"); printf("%s %s","20","NAME");;
printf("\n");
return 0;
}