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?
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.
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.
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.
There are 4 Main Types of Preprocessor Directives: Macros. File Inclusion. Conditional Compilation. Other directives.
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*/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With