鱼C论坛

 找回密码
 立即注册
查看: 3338|回复: 5

[已解决]变参宏用不了是为什么呢?

[复制链接]
发表于 2018-3-14 14:14:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 御笔剑客 于 2018-3-14 14:16 编辑
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define PR(X,...) printf("Message " #X " : "_ _VA_ARGS_ _)
  4. int main(void)
  5. {
  6.     double x=40;
  7.     double y;
  8.     y=sqrt(x);
  9.     PR(1,"x =%g\n",x);
  10.     PR(2,"x=%.2f,y=%.4f\n",x,y);

  11.     return 0;
  12. }
复制代码
最佳答案
2018-3-14 19:38:04
御笔剑客 发表于 2018-3-14 19:05
c++不是兼容c的语法吗

C++是兼容C,但并不是完全兼容,而且也不可能完全兼容
例如
  1. sh-4.4$ ls
  2. main.c
  3. sh-4.4$ cat main.c
  4. #include <stdio.h>

  5. int main(void)
  6. {
  7.         int new = 0;
  8.         printf("new: %d\n", new);

  9.         return 0;
  10. }
  11. sh-4.4$ gcc main.c
  12. sh-4.4$ ls
  13. a.exe  main.c
  14. sh-4.4$ rm a.exe
  15. sh-4.4$ mv main.c main.cpp
  16. sh-4.4$ ls
  17. main.cpp
  18. sh-4.4$ g++ main.cpp
  19. main.cpp: In function 'int main()':
  20. main.cpp:5:6: error: expected unqualified-id before 'new'
  21.   int new = 0;
  22.       ^~~
  23. main.cpp:6:25: error: expected type-specifier before ')' token
  24.   printf("new: %d\n", new);
  25.                          ^
  26. sh-4.4$
复制代码


new 不是C语言的关键字,所以可以使用 new 作为变量名
所以,C++ 并不是完全兼容C
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-3-14 15:15:43 | 显示全部楼层
__VA_ARGS__
两个下划线之间没有空格


  1. #include <stdio.h>
  2. #include <math.h>
  3. #define PR(X,...) printf("Message " #X " : "__VA_ARGS__)
  4. int main(void)
  5. {
  6.         double x=40;
  7.         double y;
  8.         y=sqrt(x);
  9.         PR(1,"x =%g\n",x);
  10.         PR(2,"x=%.2f,y=%.4f\n",x,y);

  11.         return 0;
  12. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-14 17:36:10 | 显示全部楼层
人造人 发表于 2018-3-14 15:15
__VA_ARGS__
两个下划线之间没有空格

error: unable to find string literal operator 'operator""__VA_ARGS__'
如果不带空格就会提示这个错误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-14 17:39:55 | 显示全部楼层
本帖最后由 人造人 于 2018-3-14 17:41 编辑
御笔剑客 发表于 2018-3-14 17:36
error: unable to find string literal operator 'operator""__VA_ARGS__'
如果不带空格就会提示这个错 ...


这是C语言,你用的是C++的环境吧?


  1. sh-4.4$ ls
  2. main.c
  3. sh-4.4$ cat main.c
  4. #include <stdio.h>
  5. #include <math.h>
  6. #define PR(X,...) printf("Message " #X " : "__VA_ARGS__)
  7. int main(void)
  8. {
  9.         double x=40;
  10.         double y;
  11.         y=sqrt(x);
  12.         PR(1,"x =%g\n",x);
  13.         PR(2,"x=%.2f,y=%.4f\n",x,y);

  14.         return 0;
  15. }
  16. sh-4.4$ gcc main.c
  17. sh-4.4$ ls
  18. a.exe  main.c
  19. sh-4.4$ rm a.exe
  20. sh-4.4$ mv main.c main.cpp
  21. sh-4.4$ ls
  22. main.cpp
  23. sh-4.4$ g++ main.cpp
  24. main.cpp: In function 'int main()':
  25. main.cpp:3:40: error: unable to find string literal operator 'operator""__VA_ARGS__' with 'const char [13]', 'long unsigned int' arguments
  26. #define PR(X,...) printf("Message " #X " : "__VA_ARGS__)
  27.                                         ^
  28. main.cpp:9:9: note: in expansion of macro 'PR'
  29.          PR(1,"x =%g\n",x);
  30.          ^~
  31. main.cpp:3:40: error: unable to find string literal operator 'operator""__VA_ARGS__' with 'const char [13]', 'long unsigned int' arguments
  32. #define PR(X,...) printf("Message " #X " : "__VA_ARGS__)
  33.                                         ^
  34. main.cpp:10:9: note: in expansion of macro 'PR'
  35.          PR(2,"x=%.2f,y=%.4f\n",x,y);
  36.          ^~
  37. sh-4.4$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-14 19:05:35 From FishC Mobile | 显示全部楼层
人造人 发表于 2018-3-14 17:39
这是C语言,你用的是C++的环境吧?

c++不是兼容c的语法吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-14 19:38:04 | 显示全部楼层    本楼为最佳答案   
御笔剑客 发表于 2018-3-14 19:05
c++不是兼容c的语法吗

C++是兼容C,但并不是完全兼容,而且也不可能完全兼容
例如
  1. sh-4.4$ ls
  2. main.c
  3. sh-4.4$ cat main.c
  4. #include <stdio.h>

  5. int main(void)
  6. {
  7.         int new = 0;
  8.         printf("new: %d\n", new);

  9.         return 0;
  10. }
  11. sh-4.4$ gcc main.c
  12. sh-4.4$ ls
  13. a.exe  main.c
  14. sh-4.4$ rm a.exe
  15. sh-4.4$ mv main.c main.cpp
  16. sh-4.4$ ls
  17. main.cpp
  18. sh-4.4$ g++ main.cpp
  19. main.cpp: In function 'int main()':
  20. main.cpp:5:6: error: expected unqualified-id before 'new'
  21.   int new = 0;
  22.       ^~~
  23. main.cpp:6:25: error: expected type-specifier before ')' token
  24.   printf("new: %d\n", new);
  25.                          ^
  26. sh-4.4$
复制代码


new 不是C语言的关键字,所以可以使用 new 作为变量名
所以,C++ 并不是完全兼容C
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 10:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表