What are typical uses of null statement
;
in C ?
I know that it is basically used to skip expression where it is expected by the compiler, but here I'm interested only in real-world examples of such use cases.
What are typical uses of null statement
;
in C ?
I know that it is basically used to skip expression where it is expected by the compiler, but here I'm interested only in real-world examples of such use cases.
It's typically the side-effect of a code block that was stripped by the preprocessor, like
#if DEBUG
#define ASSERT(_x) Assert(_x)
#else
#define ASSERT(_x)
#endif
ASSERT(test); // Results in null statement in non-debug builds
That, or in loops where your condition already contains whatever needs to be done in each iteration.
After a label at the end of a function (or more precisely, at the end of any block), e.g.
void foo(void)
{
// ...
exit:
;
}
I have used it, albeit rarely, in a possibly unusual situation (and one that some/many people would find wrong). I have had to sometimes write a very complex if
condition without an else
clause where the if condition has to be negated. Obviously it can be something like this:
if ( !( overly complex condition ) )
{
do stuff
}
It sometimes makes more sense (to me at least) to think of it in terms of positive logic. In other words, if the overly complex condition
holds true, I don't want the code to run. So I have instead written it as:
if ( overly complex condition )
; // do nothing
else
{
do stuff
}
A somewhat unusual use -- but for which I really appreciate the existence of the null statement -- is when I have two conditions and two actions which I find I can most naturally express like this:
if(condition1)
/* do nothing */ ;
else if(condition2)
do_something;
else do_something_else;
Often condition1
tests that everything is okay, but if it's not, condition2
distinguishes between two different exception actions do_something
and do_something_else
.
This isn't the only way to express such a thing, of course. It would be possible to repeat condition1
:
if(!condition1 && condition2)
do_something;
else if(!condition1)
do_something_else;
But that seems inferior, because it repeats condition1
. Or it would be possible to use nested if
statements:
if(!condition1) {
if(condition2)
do_something;
else do_something_else;
}
But of course nested if
statements are notoriously prone to overcomplication and obfuscation, too. So I often prefer the first version, with the null statement.
I can think of scanf
validation. scanf
gets stuck when user didn't give the correct input. So, to prevent scanf
from being stuck, characters until end of line must be removed.
if( scanf("%d",&integer) == 0 )
{
while( getchar() != '\n' ) ;
// ....
}
The only uses I can think of are:
1- At the end of a loop, where the operations are already encoded within the loop statements. e.g. while(a[i--]);
2- At the end of a label, where no operation is needed to be done. e.g. Label: ;
i was wondering how to write a null expression into the inline if and came up with this. it compiles and works.
condition ? x = 1 : "do nothing";
fun stuff.
It's more of a null expression rather than a null statement, but it's often found in for loops.
for (;;) // Loop "forever"
for (int i=10; i--; ) // 9..0
etc
The only place I use null statements is when a case
begins with a declaration:
switch(x) {
case 5: ;
int y = makeValue(z);
...
break;
...
}
This will not compile if you remove the null statement that the case begins with. The reason is that a label cannot precede a declaration.