变参宏用不了是为什么呢?
本帖最后由 御笔剑客 于 2018-3-14 14:16 编辑#include <stdio.h>
#include <math.h>
#define PR(X,...) printf("Message " #X " : "_ _VA_ARGS_ _)
int main(void)
{
double x=40;
double y;
y=sqrt(x);
PR(1,"x =%g\n",x);
PR(2,"x=%.2f,y=%.4f\n",x,y);
return 0;
} __VA_ARGS__
两个下划线之间没有空格
#include <stdio.h>
#include <math.h>
#define PR(X,...) printf("Message " #X " : "__VA_ARGS__)
int main(void)
{
double x=40;
double y;
y=sqrt(x);
PR(1,"x =%g\n",x);
PR(2,"x=%.2f,y=%.4f\n",x,y);
return 0;
}
人造人 发表于 2018-3-14 15:15
__VA_ARGS__
两个下划线之间没有空格
error: unable to find string literal operator 'operator""__VA_ARGS__'
如果不带空格就会提示这个错误{:10_266:} 本帖最后由 人造人 于 2018-3-14 17:41 编辑
御笔剑客 发表于 2018-3-14 17:36
error: unable to find string literal operator 'operator""__VA_ARGS__'
如果不带空格就会提示这个错 ...
这是C语言,你用的是C++的环境吧?
sh-4.4$ ls
main.c
sh-4.4$ cat main.c
#include <stdio.h>
#include <math.h>
#define PR(X,...) printf("Message " #X " : "__VA_ARGS__)
int main(void)
{
double x=40;
double y;
y=sqrt(x);
PR(1,"x =%g\n",x);
PR(2,"x=%.2f,y=%.4f\n",x,y);
return 0;
}
sh-4.4$ gcc main.c
sh-4.4$ ls
a.exemain.c
sh-4.4$ rm a.exe
sh-4.4$ mv main.c main.cpp
sh-4.4$ ls
main.cpp
sh-4.4$ g++ main.cpp
main.cpp: In function 'int main()':
main.cpp:3:40: error: unable to find string literal operator 'operator""__VA_ARGS__' with 'const char ', 'long unsigned int' arguments
#define PR(X,...) printf("Message " #X " : "__VA_ARGS__)
^
main.cpp:9:9: note: in expansion of macro 'PR'
PR(1,"x =%g\n",x);
^~
main.cpp:3:40: error: unable to find string literal operator 'operator""__VA_ARGS__' with 'const char ', 'long unsigned int' arguments
#define PR(X,...) printf("Message " #X " : "__VA_ARGS__)
^
main.cpp:10:9: note: in expansion of macro 'PR'
PR(2,"x=%.2f,y=%.4f\n",x,y);
^~
sh-4.4$ 人造人 发表于 2018-3-14 17:39
这是C语言,你用的是C++的环境吧?
c++不是兼容c的语法吗{:10_266:} 御笔剑客 发表于 2018-3-14 19:05
c++不是兼容c的语法吗
C++是兼容C,但并不是完全兼容,而且也不可能完全兼容
例如
sh-4.4$ ls
main.c
sh-4.4$ cat main.c
#include <stdio.h>
int main(void)
{
int new = 0;
printf("new: %d\n", new);
return 0;
}
sh-4.4$ gcc main.c
sh-4.4$ ls
a.exemain.c
sh-4.4$ rm a.exe
sh-4.4$ mv main.c main.cpp
sh-4.4$ ls
main.cpp
sh-4.4$ g++ main.cpp
main.cpp: In function 'int main()':
main.cpp:5:6: error: expected unqualified-id before 'new'
int new = 0;
^~~
main.cpp:6:25: error: expected type-specifier before ')' token
printf("new: %d\n", new);
^
sh-4.4$
new 不是C语言的关键字,所以可以使用 new 作为变量名
所以,C++ 并不是完全兼容C
页:
[1]