简体   繁体   English

应该int a,f(){}编译?

[英]Should int a, f() {} compile?

for a typo, I leave the a in there. 对于一个错字,我把a留在那里。 When I went to compile, the compiler reported: 当我去编译时,编译器报告:

missing a ',' between declaration of 'a' and 'f' 'a'和'f'的声明之间缺少','

code: 码:

int a
f(void) 
{
}

And was very surpresing since I've never get any error message like this and I didn't know I could put a comma there and it just compiled fine(in more than one compiler I tested): 因为我从来没有得到任何这样的错误信息,而且我不知道我可以在那里放一个逗号并且它编译得很好(在我测试的多个编译器中),这是非常令人惊讶的:

int a,
f(void) 
{
}

But of course it wasn't my intention but int f(void){} instead of. 但当然这不是我的意图,而是int f(void){}而不是。 I give a try in another compilers but it didn't worked and reported a syntax error like this (in clang): 我尝试了另一个编译器,但它没有用,并报告了这样的语法错误(在clang中):

error: expected ';' 错误:预期';' after top level declarator. 在顶级声明者之后。

My question is: which one is correct? 我的问题是:哪一个是正确的? it should or shouldn't compile? 它应该或不应该编译? what does standard says about that behavior? 标准对这种行为有什么看法?

I'm not going to use it since it's a bit ugly to put variable and functions split by comma (just like it is for a function prototype). 我不打算使用它,因为把变量和函数分成逗号有点难看(就像它是函数原型一样)。 I'm just curious. 我只是好奇。

EDIT: added void in function parameter. 编辑:在函数参数中添加了void Actually I wanted to write a function with empty parameters 其实我想用空参数写一个函数

That would be a syntax error. 那将是语法错误。 The syntax for a function definition is 函数定义的语法是

//        int             f()                                {}
declaration-specifiers declarator declaration-list[opt] compound-statement

There's no room for a, before f() (I'm basing this on a random C11 draft). f()之前没有空间a, (我基于随机的C11草案)。 For C89 it looks similar, except that declaration-specifiers is optional (because of the implicit-int rule). 对于C89,它看起来很相似,只是declaration-specifiers是可选的(因为implicit-int规则)。

No it should not compile 不,它不应该编译

You need 你需要

int a;
void f(void)
{
}

the voids are not the most important part at this moment in time. 在这个时刻,空隙并不是最重要的部分。 The most important part is that you didn't put a semicolon after a. 最重要的部分是你没有在分数后添加分号。

When you don't put a semicolon after int a the compiler keeps reading in the next lines as part of the int declaration, so it didn't detect that you had two things you wanted, but rather one very weird thing like 如果你没有在int a之后放一个分号,那么编译器会在下一行读取int声明的一部分,所以它没有检测到你有两件你想要的东西,而是一个非常奇怪的东西,比如

int a f() { }

By adding a comma, you still didn't fix the issue; 通过添加逗号,您仍然无法解决问题; because, a comma indicates a list which would be a valid way to declare a list of integers 因为,逗号表示一个列表,它是声明整数列表的有效方法

int a, b;

is valid while 是有效的

int a, f() { }

is not because f() { } is not a valid name for an integer. 不是因为f() { }不是整数的有效名称。 Characters like that might be valid in other places, but the preceding a, prevents it from being a valid there either. 像这样的字符可能在其他地方有效,但前面的a,阻止它在那里有效。 The combination of what was meant to be two lines prevents it from being valid C. 什么意味着两行的组合阻止它有效C.

But by adding a semicolon, now you get two sensible things in C. 但是通过添加分号,现在你在C中得到了两个明智的东西。

int a;
f() { }

I highly recommend that every function you write has a return value, even if it returns nothing. 我强烈建议你编写的每个函数都有一个返回值,即使它什么都不返回。

int a;
void f() { }

and again, it is a very good practice to explicitly indicate that the function doesn't take any parameters, so 再次,明确指出该函数不带任何参数是一个非常好的做法,所以

int a;
void f(void) { }

which is equivalent to 这相当于

int a;
void f(void)
{
}

The language grammar does not allow for a function definition to appear as part of a declaration 1 . 语言语法不允许函数定义作为声明 1的一部分出现。 What you can do is write 可以做的是写

int a, f(void);

because this is only a declaration for f . 因为这只是f声明


1. That is, there's no path from the declaration non-terminal to the function-definition non-terminal; 也就是说,从非终端声明功能定义非终端没有路径; see Appendix A of the online C standard 在线C标准的附录A.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM