3

I want to know exactly, what is a null statement in C programming language? And explain a typical use of it.

I found the following segment of code.

for (j=6; j>0; j++)
;

And

for (j=6; j>0; j++)
Elliyas Ahmed
  • 101
  • 1
  • 1
  • 10
  • An example might be `while (1 == 1) ;` which loops forever doing nothing. Use it, for instance, in a thread which you do not want to terminate, which might continue to process messages which it receives, or hanldle interrupts, or have its callback functions called by other processes. – Mawg says reinstate Monica Nov 05 '16 at 16:14
  • 1
    Closed -- unclear? Booo. Ok so the question shows lack of research but that doesn't justify closing it with an incorrect reason. – Joshua Nov 05 '16 at 17:55
  • Possible duplicate of [Use of null statement in C](http://stackoverflow.com/questions/5599380/use-of-null-statement-in-c) – Cody Gray - on strike Jan 11 '17 at 16:06

6 Answers6

12

From the msdn page:

The "null statement" is an expression statement with the expression missing. It is useful when the syntax of the language calls for a statement but no expression evaluation. It consists of a semicolon.

Null statements are commonly used as placeholders in iteration statements or as statements on which to place labels at the end of compound statements or functions.

know more: https://msdn.microsoft.com/en-us/library/1zea45ac.aspx


And explain a typical use of it.

When you want to find the index of first occurrence of a certain character in a string

int a[50] = "lord of the rings";
int i;

for(i = 0; a[i] != 't'; i++)
    ;//null statement
//as no operation is required
Cherubim
  • 5,287
  • 3
  • 20
  • 37
  • I get warning suggest braces around empty body in an 'if' statement for if(a); but I do not get warning for the usage if(a){}. What is the difference? – Rajesh Mar 14 '20 at 16:26
6

A null statement is a statement that doesn't do anything, but exists for syntactical reasons.

while ((*s++ = *t++))
    ; /* null statement */

In this case the null statement provides the body of the while loop.

or (disclaimer: bad code)

if (condition1)
    if (condition2)
        dosomething();
    else
        ; /* null statement */
else
    dosomethingelse();

In this case the inner else and null statement keeps the outer else from binding to the inner if.

Joshua
  • 40,822
  • 8
  • 72
  • 132
4

From C11, §6.8.4.1, 6.8.3 Expression and null statements:

A null statement (consisting of just a semicolon) performs no operations.

The standard also provides a couple of common uses of it:

EXAMPLE 2 In the program fragment

      char *s;
      /* ... */
      while (*s++ != '\0')
              ;

a null statement is used to supply an empty loop body to the iteration statement.

6 EXAMPLE 3 A null statement may also be used to carry a label just before the closing } of a compound statement.

      while (loop1) {
            /* ... */
            while (loop2) {
                    /* ... */
                    if (want_out)
                            goto end_loop1;
                    /* ... */
            }
            /* ... */
      end_loop1: ;
      }
P.P
  • 117,907
  • 20
  • 175
  • 238
0

A null statement doesn't DO anything, hence the solo ';'

so....this code block will do nothing 10x in C/C++

for(int i = 0; i < 10; i++){//execute the following 10 times
   ;//nothing aka "null statement"
}//end for
Tapper7
  • 173
  • 8
0

Sometimes the null statement serves. In the "old days" the math hardware might not return an integer value when you needed one, such as returning 0.999... as sin(pi / 2), (that is sin(90 degrees)).

int i, j, k;
i = something;
for(j = 1; (2 * j) <= i; j *= 2);

Computes in j the largest integer power of 2 less than i. Not very efficient for large i but useful if you could not trust Log functions to return an exact integer value.

My C++ compiler does not accept the null statement as a function body, but does accept the "empty statement" which I think is just an empty statement block "{}".

Tadzu
  • 9
  • 2
0

Whether C or java, null has a special significance. Whenever there is a need of re-intializing of values to variavle, null serves the purpose.

If there is a String i, and we want it to add a char value through a loop, then first we have to intialize i with null.

  • The OP's question was about null ***statements***, rather than the null ***keyword***, which is what your answer appears to address. – Adrian Mole Jun 12 '21 at 20:32