鱼C论坛

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

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

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

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

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

x
本帖最后由 御笔剑客 于 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;
}
最佳答案
2018-3-14 19:38:04
御笔剑客 发表于 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.exe  main.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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-3-14 15:15:43 | 显示全部楼层
__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;
}
想知道小甲鱼最近在做啥?请访问 -> 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__'
如果不带空格就会提示这个错误
想知道小甲鱼最近在做啥?请访问 -> 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++的环境吧?

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.exe  main.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 [13]', '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 [13]', '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$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

c++不是兼容c的语法吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-14 19:38:04 | 显示全部楼层    本楼为最佳答案   
御笔剑客 发表于 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.exe  main.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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-1 17:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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