if (1 == 1) printf("one is one\n"); printf("is this inside the if statement??/who kn0WS\n");
The second printf() should never be executed inside an if .
The reason is that the previous line ends with a semicolon, which indicates the end of the executed if-block.
(I mean, the whole reason for a semicolon is an all-expression-thing so that the parser can cut out \ n, \ t literal spaces and still know where each statement ends, right?)
So, how can one unambiguously parse the previous bit of code (or maybe someone can come up with a better example of what I mean), if you ignore the spaces?
Analysis Example :
if (1 == 1) // , if - ( and ) - instructions (or block), skip all spaces
// no { found β one operator, scan to ; (external quotes / comments)
printf("one is one\n"); // ; , end of if block
Without curly braces, only one statement refers to an if block.
But, as already mentioned, itβs a good habit to use braces. If you add an instruction later (for example, a quick temporary printf() ), it will always be inside the block.
Special occasion :
int i = 0; while(i++ < 10); printf("%d", i);
Here printf() will be executed only once. Note ; at the end of while() .
In the case of an empty instruction, it is better to use:
while(i++ < 10) ;
to make the intent understandable (or, alternatively, you can use the empty block {} ).