5

for a typo, I leave the a in there. When I went to compile, the compiler reported:

missing a ',' between declaration of 'a' and '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. I give a try in another compilers but it didn't worked and reported a syntax error like this (in 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. Actually I wanted to write a function with empty parameters

Jack
  • 16,276
  • 55
  • 159
  • 284
  • 2
    The former _seems_ to be valid C89, and the latter invalid. For C99, I'm pretty sure, both are invalid (and gcc refuses to compile with `-std=c99 -pedantic`). `int a, f();` (the latter being a function declaration (function taking unspecified number of arguments returning `int`) without definition) however, would be valid C99. Which standard are you interested in? – mafso Jul 11 '14 at 17:29
  • Nice to know, I'll remove my comment, as it really doesn't mean to say any more than yours did, but somehow in the re-reading, you said it more cleanly than I. – Edwin Buck Jul 11 '14 at 17:54

3 Answers3

4

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). For C89 it looks similar, except that declaration-specifiers is optional (because of the implicit-int rule).

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
3

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 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. Characters like that might be valid in other places, but the preceding a, prevents it from being a valid there either. The combination of what was meant to be two lines prevents it from being valid C.

But by adding a semicolon, now you get two sensible things in 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)
{
}
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • Is this `C89` function definition? – Fiddling Bits Jul 11 '14 at 17:27
  • C99 doesn't allow an empty declaration at file scope (as in your two second-last examples, the `;` after the closing `}` of `f`'s function body). And you're talking about _declarations_, not statements (there aren't statements at file scope). – mafso Jul 11 '14 at 17:33
  • @FiddlingBits: `void f() { }` is a valid function definition in C89/C90, in C99, and in C11. In all three versions of the language, `void f(void) { }` is preferred. (The `void` keyword and prototype syntax were introduced in C89.) – Keith Thompson Jul 11 '14 at 17:37
  • I'll add in the parameter void, best to not let old practices harm the new programmers. – Edwin Buck Jul 11 '14 at 17:42
0

The language grammar does not allow for a function definition to appear as part of a declaration1. What you can do is write

int a, f(void);

because this is only a declaration for 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
John Bode
  • 119,563
  • 19
  • 122
  • 198