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

Go To StackoverFlow.com

4

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?

2012-04-04 23:57
by FatalError


1

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.

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*/   
2012-04-05 00:08
by Michael Graczyk
The preprocessor normally removes all comments as well.. - Chris Dodd 2012-04-05 00:40
Well I suppose it could be used to make certain that a certain comment does not appear in the preprocessed code - Michael Graczyk 2012-04-05 05:28
This made me realize an interesting, albeit obscure, potential for usage. If we're using cpp utility to make multiple passes through a file, like for code generation, it could be used to separate comments for the first pass from comments that should appear in the generated code. There is a -C flag that asks cpp to preserve comments, but these comments on Null directive lines are still discarded - FatalError 2012-04-09 17:34
@FatalError Yes that was the point of my answer - Michael Graczyk 2012-04-11 02:04


2

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.

2012-04-05 00:01
by P.P.
I take that to mean you can use # on its own line to add vertical space to the original source code without affecting the preprocessor output. Perhaps for readability - Adam Liss 2012-04-05 00:02
I guess so. It also states "A null directive is understood as a preprocessing directive but has no effect on the preprocessor output." I don't see any other purpose other than readability. Perhaps, a thing came from Unix shell script like a comment. - P.P. 2012-04-05 00:07
Ads