The NULL Statement
In this tutorial, we will understand the NULL statement which is unique in its own way. Let’s dive in and understand its usefulness.
In this tutorial, we will understand the NULL statement which is unique in its own way. Let’s dive in and understand its usefulness.
Consider the following for statement:
for (i = 0; ; i++)
In the above line, within parentheses, there are three statements – initialization statement, the null statement (represented by semicolon), and the increment statement.
The null statement is an empty statement represented by a semicolon. It doesn’t perform any actions, but it is useful to fill the place of some statements which we don’t want to put, but it is mandatory to put just like the above for statement where adding a conditional statement is mandatory, but if for some reason, we don’t want to specify the condition, we can put the null statement in place.
As mentioned, the NULL Statement is needed for syntactical reasons. In situations where a statement is grammatically required but no actual work needs to be done, the NULL Statement steps in. It acts as a placeholder ensuring that the syntax remains valid.
Consider the following example:
for (int i = 0; i < 10; i++) ;
The above for loop is a waste. It doesn’t do anything as the loop body is missing. Have you noticed the semicolon at the end? This is acting like a placeholder for the loop body.
Suppose that in a text our job is to find the position of the first occurrence of the letter ‘m’. Here’s how we can achieve this programmatically:
// Program to find the position of the first occurrence of letter 'm'
#include <stdio.h>
int main()
{
char a[] = "I love programming at PUB";
int pos;
for (pos = 0; a[pos] != 'm'; pos++) ;
printf("The first occurrence of letter 'm' is at position %d", pos+1);
return 0;
}
Output:
The first occurrence of letter 'm' is at position 14
In the above program, the main highlight is the for loop which continues to read the characters of the given string until it encounters the letter ‘m’. As variable ‘pos’ is declared outside the for loop, its value is retained within the printf function, and hence the output.
Question 1: Is there any alternative to the NULL Statement?
Answer: Some programmers prefer to write the dummy continue statement as follows:
for (int i = 0; i < 10; i++)
continue;
Some prefer to use the empty blocks like the following:
for (int i = 0; i < 10; i++)
{}
These are some alternatives to the NULL statement.
Question 2: How to write a while loop that runs forever but does nothing?
Answer: Simple! Just use the following line of code:
while (1) ;
That’s it.
Question 3: What is the output of the following code?
#include <stdio.h>
int main()
{
int i;
for (i = 1; i % 3 != 0; i++) ;
{
printf("%d ", i);
}
return 0;
}
Answer: If your answer is 1 2, think again! The output of the above program is 3. If you observe carefully, the entire block represented by curly braces and the printf()
is not part of the for loop. The for loop has an empty body because the for statement ends with a semicolon. I have mentioned this through a comment as follows:
#include <stdio.h>
int main()
{
int i;
for (i = 1; i % 3 != 0; i++) ; // Ends with semicolon
{
printf("%d ", i);
}
return 0;
}
Notice that these types of errors are barely noticeable and hence difficult to debug. Therefore, please be careful when writing your code.
In summary, the NULL statement may look like a waste, but it’s not. The NULL statement plays a crucial role in maintaining the correctness of the syntax. Remember that it is not meant to execute a piece of code but to keep things grammatically sound in programming.
Leave a comment