开发者

Is it possible to execute a "C" statement without a semicolon

开发者 https://www.devze.com 2022-12-10 22:15 出处:网络
Post an example to execu开发者_高级运维te a \"C\" statement without semicolon( ; )This line is a statement:

Post an example to execu开发者_高级运维te a "C" statement without semicolon( ; )


This line is a statement:

while (0) { }


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.


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) {}
}


Use this function:

__asm {
     mov al, 2
     mov dx, 0xD007
     out dx, al
}


{ }

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


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


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);
}


#define _ ;

int main()
{
   return 0 _
}
0

精彩评论

暂无评论...
验证码 换一张
取 消