What are null satements in C? what are the consequences of null statement in c?
What are null satements in C? what are the consequences of null statement in c?
The null statement performs no operation. It has the form:
>>-;-----------------------------------------------------------><
A null statement can hold the label of a labeled statement or complete the syntax of an iterative statement.
Examples of Null Statements
The following example initializes the elements of the array price. Because the initializations occur within the for expressions, a statement is only needed to finish the for syntax; no operations are required.
for (i = 0; i < 3; price[i++] = 0) ;
A null statement can be used when a label is needed before the end of a block statement. For example:
void func(void) {
if (error_detected)
goto depart;
/* further processing */
depart: ; /* null statement required */
}
Hi!
The Null statement is nothing but, There is no body within loop or any other statements in C.
This Statement is used for some time delay or the COMMENT /*C PROGRAM1*/ is also a null statement.
This example illustrates the null statement:
eg :1)
for ( i = 0; i < 10; line[i++] = 0 )
;
eg :2)
while(i++<10);
Regards
Venkat.G
Hi!
NULL statement is a statement, it end with semicolon ( ; or {without any body}) wherever we required,we can use this statement for some Time delay.
if(1);
for(i=0;i<10;i++);
while(i++<10)
{
}
Regards
Venkat.G
Nice succint explanation. I have one comment and one question on this topic.
1.
The for() loop example is a dangerous programming practice. This should never be used as it is very easy to miss the NULL statement and misunderstand the program i.e. readability is shot. So, a better way would be
for (....)
/* Null statement */;
Note the semicolon at the end. Judging by your answer, I think you knew that already.
2.
While thinking about the above point, a question occurred to me. Is there a practical use for this NULL statement?? I mean I just can't remember every using it in a professional capacity. If you have used it, I would appreciate some examples. Thanks.
A "null statement" is a statement containing only a semicolon; it can appear wherever a statement is expected. Nothing happens when a null statement is executed. The correct way to code a null statement is :
Hi!
NULL statement is a statement, it end with semicolon ( ; or {without any body}) wherever we required,we can use this statement for some Time delay.
if(1);
for(i=0;i<10;i++);
while(i++<10)
{
}