Post an example to execute a "C" statement without semicolon( ; )
-
What is this? Homework?– S.LottNov 6, 2009 at 13:36
-
That's no a good question.. please be more specific! void main(void) {} -- A statement without ';'– ShirkrinNov 6, 2009 at 13:37
-
1Why close this? Why "not a real question"? It does seem like one to me.– R. Martinho FernandesNov 6, 2009 at 13:44
-
4It's a question: "Is it?". It's programming related (related to the C language). I may be a candidate for CW, but it is a legitimate question.– R. Martinho FernandesNov 6, 2009 at 13:50
-
5I see a dangerous (I hope occasional) tendency in this community to close as not a real question when the question is too useless from a "real code" point of view, or too "newbie". Please let's try not to create a stronghold. I've seen this happen already too many times. If this is homework, so be it, we are not there to look into people's decision not to do their homeworks, except when blatant. This is a legitimate question, and deserves a legitimate answer, not -1 and not 4 closes. Thanks.– Stefano BoriniNov 6, 2009 at 13:55
9 Answers
This line is a statement:
while (0) { }
-
4how 'bout
while (1) { }
? At least you get some work done :) Nov 6, 2009 at 13:42 -
And there's another interesting fact about the
1
vs0
. The one with the0
can be optimized away to nothing, while the one with the1
cannot. Nov 6, 2009 at 14:14 -
1
-
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. Nov 6, 2009 at 20:24
You can an expression in an if()
as long as it evaluates to a scalar (integer, floating point number or pointer).
if (expr, 0) {}
According to the C grammar, expr
is an expression. if(expr){}
is a selection_statement
, so this would match the bill.
Note that the ,0
isn't always necessary since the body of the if()
is empty. So these would be equivalent statements, if expr
returns a scalar:
if (expr) {}
while (expr, 0) {}
while (expr && 0) {}
while (expr || 0) {}
All would evaluate the expression once.
-
1That won't make it a statement, but an expression. The whole thing (the if and the braces), is a statement, though. Nov 6, 2009 at 13:43
-
-
-
No, a function call is an expression which evaluates to its return value. Nov 6, 2009 at 14:03
-
1+1 This answer provides a statement and a placeholder for doing things. Nov 6, 2009 at 14:06
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) {}
}
-
1Hmm, don't see a statement there - just a declaration and an empty function body.– cafNov 6, 2009 at 13:37
-
3
-
The body if main is a (compound) statement according to the C grammar. Nov 6, 2009 at 13:39
-
Only the body is a statement. The whole of the code posted is a function definition. Nov 6, 2009 at 13:40
-
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. Nov 6, 2009 at 13:43
Use this function:
__asm {
mov al, 2
mov dx, 0xD007
out dx, al
}
-
6
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) {}
}
-
the why is backward compatibility, I guess. It will just issue a warning. Nov 6, 2009 at 14:00
-
3Not quite. In C++, if main() doesn't have a return statement, it's guaranteed to return 0. Nov 6, 2009 at 14:06
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);
}