10

In the C programming language, it is possible to omit a code block in the case of a single statement, for example:

if(1) exit();

Now, does this only apply to conditionals ? Why is this not valid in the case of functions:

void f(int a) exit();
5
  • 4
    Because the standard says so? Commented Jul 7, 2011 at 14:40
  • 1
    never tried it, but I suppose that, if it is not allowed, it is just a matter of syntax, maybe legacy: once you would have written void f(a) int a; { ... }, so the { } were needed to mark the end of argument type declaration block ... maybe... Commented Jul 7, 2011 at 14:42
  • thanks, I knew of the old function syntax, but this also means compilers are able to correctly parse this then.. Commented Jul 7, 2011 at 14:48
  • compilers are able to correctly parse that (at least, gcc can). Commented Jul 7, 2011 at 14:59
  • See this related question, asking the same question about C#: stackoverflow.com/questions/6016654/… Commented Jul 7, 2011 at 16:29

3 Answers 3

20

It's a feature of C syntax. In BNF, a function definition is something like

FUNC_DEF ::= TYPE IDENTIFIER "(" PARAM_LIST ")" BLOCK

while a statement is

STATEMENT ::= (EXPRESSION | DECLARATION | CONTROL | ) ";" | BLOCK
BLOCK ::= "{" STATEMENT* "}"

(simplifying to allow intermixed declarations and statements, which C++ allows but C doesn't), and an if statement is

CONDITIONAL ::= "if" "(" EXPRESSION ")" STATEMENT

omitting the else part for now.

The reason for this is that otherwise, you could write the function

void no_op() {}

as

void no_op();

but the latter syntax is already in use to denote a declaration.

Sign up to request clarification or add additional context in comments.

6 Comments

I think it's intrasting question from the C parser point of view. Because I dont see any contradiction in the second statment for parser. But any way +1
Thanks ! This is exactly the kind of answer I was looking for, and you remind me why I like CS !
The int no_op() {} as no_op() looks more confusing.
Though you could forbid the void no_op(); syntax for definitions, e.g. FUNC_DEF ::= TYPE IDENTIFIER "(" PARAM_LIST ")" (BLOCK|STATEMENT). But that would be really confusing to newbies and even apprentices.
@phresnel: I updated the STATEMENT rule to more accurately reflect C syntax; ; by itself is a valid statement, so your suggestion would not work.
|
4
  • The syntax of a conditional statement is this:

    if(expression) statement
    
  • A compound statement is a statement.

  • A compound statement is defined as

    { zero or more statements }
    
  • The syntax of a function definition is this

     function_declaration compound_statement
    
  • So, by definition a function body must be a compound statement and have {}

  • QED :)

Comments

1

There is a very old dialect of C, the K&R C. In this dialect the function declaration may look like this:

fun_a(a,b)
char a;
float b;
{
        fun_b(b,a);
}

I think it would be too hard to parse it without { and }.

1 Comment

This is probably the historical reason, +1, but there's also a reason why the syntax hasn't changed in ANSI C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.