Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of the C preprocessor's Null directive?

Tags:

c

standards

So, poking through the n869 draft of the C99 standard I stumbled across this section:

6.10.7 Null directive Semantics

A preprocessing directive of the form

# new-line

has no effect.

So, I wrote this program to test it out:

#
#include <stdio.h>
#

int main(void)
{
  puts("Hello, world!");

  return 0;
}

Sure enough, gcc had no beef with this code, even when I cranked up warnings and such all the way. I realize there are some other constructs in the language that aren't obvious, like the extra comma allowed in initializers, enum defs etc., but that has a purpose (e.g. simplifies writing a code generator).

However, I don't see how this one is useful. Can anyone think of a reasonable use case / rationale for having it at all?

like image 472
FatalError Avatar asked Apr 04 '12 23:04

FatalError


People also ask

What is the purpose of preprocessor directives?

Preprocessor directives, such as #define and #ifdef , are typically used to make source programs easy to change and easy to compile in different execution environments. Directives in the source file tell the preprocessor to take specific actions.

What is null directive?

The null directive (#) The null directive performs no action. It consists of a single # on a line of its own. The null directive should not be confused with the # operator or the character that starts a preprocessor directive. In the following example, if MINVAL is a defined macro name, no action is performed.

What is the use of preprocessor directive in C language?

It is a pre-process of execution of a program using c/c++ language. To initialize a process of preprocessor commands, it's mandated to define with a hash symbol (#). It can preferably be the non-blank character, and for better readability, a preprocessor directive should start in the first column.

What are the 4 types of preprocessor directives?

There are 4 Main Types of Preprocessor Directives: Macros. File Inclusion. Conditional Compilation. Other directives.


2 Answers

From the GCC docs, section 1.7:

The null directive consists of a #' followed by a Newline, with only whitespace (including comments) in between. A null directive is understood as a preprocessing directive but has no effect on the preprocessor output. The primary significance of the existence of the null directive is that an input line consisting of just a#' will produce no output, rather than a line of output containing just a `#'. Supposedly some old C programs contain such lines.

  • http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html

Remember that the C pre-processor is a program in its own right, and it has input and output. The output of the C preprocessor normally includes program comments, but if the comments appear on a line which begins with the "#" symbol, and has no content except for whitespace and comments, then the comments will not appear in the preprocessor output. So the null directive results in content being present in the source code, but not in the preprocessor output.

Examples:

The preprocessor will convert

#include <stdio.h>
#define HELLO 1
# /*This comment is for preprocessor only*/
HELLO
/*This comment is for preprocessed code*/

into

(... preprocessed contents of stdio.h ...)
1
/*This comment is for preprocessed code*/   
like image 107
Michael Graczyk Avatar answered Oct 05 '22 11:10

Michael Graczyk


From GNU doc:

.....The primary significance of the existence of the null directive is that an input line consisting of just a #' will produce no output, rather than a line of output containing just a#'. Supposedly some old C programs contain such lines.

like image 45
P.P Avatar answered Oct 05 '22 11:10

P.P