Post an example to execute a "C" statement without semicolon( ; )
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) {}
}
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.
{ }
At least 15 characters are required to post an answer...
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) {}
}
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);
}