2

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

13
  • What is this? Homework?
    – S.Lott
    Nov 6, 2009 at 13:36
  • That's no a good question.. please be more specific! void main(void) {} -- A statement without ';'
    – Shirkrin
    Nov 6, 2009 at 13:37
  • 1
    Why close this? Why "not a real question"? It does seem like one to me. Nov 6, 2009 at 13:44
  • 4
    It'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. Nov 6, 2009 at 13:50
  • 5
    I 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. Nov 6, 2009 at 13:55

9 Answers 9

10

This line is a statement:

while (0) { }
4
  • 4
    how '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 vs 0. The one with the 0 can be optimized away to nothing, while the one with the 1 cannot. Nov 6, 2009 at 14:14
  • 1
    for (;;) {} .. crap, coded myself right out of a clever comment.
    – Tim Post
    Nov 6, 2009 at 14:27
  • 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
8

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.

7
  • 1
    That 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
  • Isn't a function call a statement, too? Nov 6, 2009 at 13:52
  • when used inside an expression, it is an expression. Nov 6, 2009 at 14:02
  • 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.
    – mouviciel
    Nov 6, 2009 at 14:06
3

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) {}
}
8
  • 1
    Hmm, don't see a statement there - just a declaration and an empty function body.
    – caf
    Nov 6, 2009 at 13:37
  • 3
    A function body consists of a compound statement.
    – CB Bailey
    Nov 6, 2009 at 13:38
  • 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.
    – CB Bailey
    Nov 6, 2009 at 13:43
3

Use this function:

__asm {
     mov al, 2
     mov dx, 0xD007
     out dx, al
}
1
  • 6
    Not a C statement, but a proprietary compiler directive.
    – gnud
    Nov 6, 2009 at 13:52
2

{ }

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

1
if (i=2) {} // give `i` a value
1

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) {}
}
2
  • the why is backward compatibility, I guess. It will just issue a warning. Nov 6, 2009 at 14:00
  • 3
    Not quite. In C++, if main() doesn't have a return statement, it's guaranteed to return 0. Nov 6, 2009 at 14:06
1
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);
}
0
#define _ ;

int main()
{
   return 0 _
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.