其他分享
首页 > 其他分享> > C: 编译错误:expected identifier or ‘(’ before numeric constant

C: 编译错误:expected identifier or ‘(’ before numeric constant

作者:互联网

文章目录

错误

字面意思,期望一个变量名(标识符),或者在数字常量前面期望一个左括号。

macro.c: In function ‘main’:
macro.c:9:6: error: expected identifier or ‘(’ before numeric constant
  int PIPE_BUF = 0;
      ^~~~~~~~

原因

在include linux/limits.h 之后,定义了一个PIPE_BUF 变量。但是在 limit.h 头文件里已经有一个PIPE_BUF定义,这个变量在预编译阶段,就会被替换成这个宏定义。出现下面的情况。

#include <linux/limits.h>
#define STR(x) #x
#define XSTR(x) STR(x)
int main (void)
{
 int PIPE_BUF = 0;
 assert (strcmp ("PIPE_BUF", XSTR (PIPE_BUF)) == 0);
 return 0;
}

==== 预编译结果
# 7 "macro.c"
int main (void)
{
 int
# 9 "macro.c" 3 4
    4096    、、、、 这里搞了替换,就能明白为什么期望一个括号了,因为这个替换之后是个数字常量。
# 9 "macro.c"  
             = 0;

# 10 "macro.c" 3 4
((void) sizeof ((
# 10 "macro.c"
strcmp ("PIPE_BUF", "4096") == 0
# 10 "macro.c" 3 4
) ? 1 : 0), __extension__ ({ if (
# 10 "macro.c"
strcmp ("PIPE_BUF", "4096") == 0

标签:10,constant,4096,int,macro,numeric,PIPE,expected,BUF
来源: https://blog.csdn.net/qq_36428903/article/details/121890696