tags:

views:

413

answers:

9

Post an example to execute a "C" statement without semicolon( ; )

+2  A: 

Wrong answer

... with a new right answer below.

int main(void)
{
}

The pair of braces in the definition of main is a compound-statement which is one of the valid forms for a statement.

Edit: although a statement can be a compound-statement, and a function-body consists of a compound-statement, when the compound-statement is a function-body, it's not a statement.

Edit, Edit:

This program does contain a statement which is executed, though:

int main(void)
{
    if (1) {}
}
Charles Bailey
Hmm, don't see a statement there - just a declaration and an empty function body.
caf
A function body consists of a compound statement.
Charles Bailey
The body if main is a (compound) statement according to the C grammar.
Aaron Digulla
Only the body is a statement. The whole of the code posted is a function definition.
Martinho Fernandes
Oh, I get you. Although a _compound statement_ can be a _function-body_ or a _statement_, when it's a _function-body_ it's not technically a _statement_.
Charles Bailey
Dont delete! What you said in the answer is not wrong!
Martinho Fernandes
I was trying to post an example which was a complete program, most of the other answers were posting snippets from which it's not trivially obvious that execution of the statement can occur with adding a `;` somewhere to complete the program.
Charles Bailey
The pair of braces *is* a statement. The function header plus the braces is not.
Martinho Fernandes
Not it's not _statement_, only a _compound-statement_. A _compound-statement_ can either be a _statement_ or a _function-body_, not both at the same time.
Charles Bailey
C never surprises to cease me...
Martinho Fernandes
+10  A: 

This line is a statement:

while (0) { }
caf
how 'bout `while (1) { }`? At least you get some work done :)
Martinho Fernandes
@Martinho: too tiring.
Steve Jessop
And there's another interesting fact about the `1` vs `0`. The one with the `0` can be optimized away to nothing, while the one with the `1` cannot.
Martinho Fernandes
for (;;) {} .. crap, coded myself right out of a clever comment.
Tim Post
There was a very long and tedious discussion on news group comp.std.c about whether the compiler should be able to optimize away useless loops like the 'while (1) { }' loop.
Jonathan Leffler
+6  A: 

You can put your statement in an if() as long as it doesn't evaluate to void.

if (statement, 0) {}

[EDIT] According to the C grammar, the "statement" above is in fact an expression. if(expr){} is a selection_statement, so this would match the bill.

Note that the ,0 isn't really necessary since the body of the if() is empty. So these would be equivalent statements:

if (expr) {}
while (expr, 0) {}
while (expr && 0) {}
while (expr || 0) {}

All would evaluate the expression once.

Aaron Digulla
That won't make it a statement, but an expression. The whole thing (the if and the braces), is a statement, though.
Martinho Fernandes
Isn't a function call a statement, too?
Aaron Digulla
when used inside an expression, it is an expression.
Martinho Fernandes
No, a function call is an expression which evaluates to its return value.
Jurily
+1 This answer provides a statement and a placeholder for doing things.
mouviciel
A function call only "becomes" a statement when you add the precious semicolon.
Martinho Fernandes
Okay, i checked the C grammar. An "expression statement" is an expression followed by ";", so an expression isn't also a statement unless it's terminated by ";". But "if(expr){}" is a statement.
Aaron Digulla
+4  A: 
__asm {
     mov al, 2
     mov dx, 0xD007
     out dx, al
}
Jurily
Not a C statement, but a proprietary compiler directive.
gnud
+1  A: 
if (i=2) {} // give `i` a value
Nick D
+2  A: 

{ }

At least 15 characters are required to post an answer...

Martinho Fernandes
+1  A: 

Even whole program (my GNU C built it despite result code returned is undefined). The question is WHY?

/* NEVER DO THIS!!! */
int main()
{
    {}
}

And in C++ we even can stabilize return code by this simple stack trick with variable (yes, it is dirty, I understand but I think it should work for most cases):

/* NEVER RELY ON SUCH TRICKS */
int main()
{
   if (int i=0) {}
}
Roman Nikitchenko
the why is backward compatibility, I guess. It will just issue a warning.
Stefano Borini
Oops... should be 'why we should code something like this'...
Roman Nikitchenko
+1 for the stack trick.
Martinho Fernandes
Not quite. In C++, if main() doesn't have a return statement, it's guaranteed to return 0.
Jurily
+1  A: 
int main()
{
  // This executes a statement without a semicolon
  if( int i = 10 )
  {      
    // Call a function
    if( Fibonacci(i) ) {}
  }

  // I have made my point
  return 0;
}

int Fibonacci(int n)
{
  return (n == 2) ? 1 : Fibonacci(n - 2) + Fibonacci(n - 1);
}
Yannick M.
+1  A: 
#define _ ;

int main()
{
   return 0 _
}
Peter van der Heijden