This blog is under construction

Sunday, 29 December 2013

Null statement

If a statement has only semicolon, then it is called as null statement.


What is the purpose of null statement?
do nothing

Example 1:
for (i = 0; i < 5; i++)
;   // null statement
Here, the body of the for loop is null statement.

Example 2:
goto label;
Program needs at least one statement below label.  If we have no valid statement to place below label, then we can put null statement below label as shown above.

Example c program using null statement
  #include <stdio.h>
  main() {
        int i;
        for (i = 0; i < 5; printf("Hello world\n"), i++)
                ;  // null statement
        if (i == 5)
                goto err;
        printf("You can't execute me!!");
        printf(" Yahoooooooooo!!!\n");
                ; // null statement behind label

  jp@jp-VirtualBox:~/$ ./a.out
  Hello world
  Hello world
  Hello world
  Hello world
  Hello world

No comments:

Post a Comment