Tuesday, February 14, 2012

Write a C program with out using semicolon


The logic for this is tricky. Generally C language parse the code as expressions. Compiler checks for the valid expressions. If the expression is invalid , it will throw compilation error. It applies for the condition checking also. here comes the trick. see below example.

int main()
{
if(printf("world\n"))
{
//nothing
}
}

In the above code, it checks for the valid expression in if statement. It will check for the validity of the expresion and not the result/return value of the expression. So it will display the message. you can give any valid expression for condition check.


Result of the above code:

practice 1813> gcc nosemicolon.c
practice 1814> ./a.out
world
practice 1815>

No comments:

Popular Posts