Please consider that this intrusive sentence could instead be proclaiming that you help sponsor this research.

Home > Programming > Tools > Visual C++ > Language > Preprocessor

Preprocessor Directives

The # sign has special significance as introducing a directive for the preprocessor’s tokenisation of the source code.

Syntax

# [directive [ arguments ]]

From the start of the line, there may be any amount of white space, including none, before the # sign. A # sign that is not the first non-trivial character on its line is not an error to the preprocessor, and is passed without complaint to the preprocessed output generated by the /E option, but will ultimately trigger an error (typically C2014, but C2121 if the # sign occurs in a macro expansion).

The # sign may be followed by any amount of white space, including none, before the optional directive, which consists of as many of the subsequent characters as satisfy the syntax of an identifier. It is an error (C2019) if the first character of directive is not valid for starting an identifier. It is a fatal error (C1021) if directive is not the name of a supported directive (as listed below).

Examination of characters on the way to identifying directive is done with reduction of trigraph escape sequences, splicing of line continuations and discarding of comments, but without macro expansion. Note in particular that comments in preprocessor directives are even ignored when the /C option otherwise passes comments into the preprocessor output generated for /E.

Characters that follow directive are arguments for the directive. Interpretation, including the elimination of comments and the recognition of macros, varies from one directive to another.

Supported Directives

In the following list of preprocessor directives supported by Microsoft Visual C++ version 13.00.9466, those that seem to be omitted from the product documentation are highlighted yellow.

In addition, #bimport is recognised but rejected, so that it produces the fatal error C1021, just as for unrecognised text.

Coding Error

The function that examines the remainder of a line after an initial # sign has a coding error (of admittedly little or no practical consequence) concerning the treatment of a forward slash, backslash or question mark where an identifier is expected for naming the directive.

What distinguishes these characters is that they each may, yet need not, start a sequence of some special significance. A forward slash may be followed immediately by another to introduce a single-line comment or by an asterisk to introduce a C-style comment, but if followed by anything else, including by nothing, a slash is just a slash. A backslash may be a line-continuation character. A question mark may introduce a trigraph escape sequence.

If not introducing their special sequences, these characters are surely invalid where a preprocessor directive is expected. They might be expected to trigger error C2019. Instead, the code proceeds as if it has not only found an identifer to name the directive but has already processed it. The identifier processed most recently, if any, becomes the directive and the characters that follow the slash become arguments for that directive.

For a demonstration, compile

int pragma;

 

# / message ("This surely ought not work.")

and see the compiler display the quoted message exactly as if given #pragma message as a directive. Indeed, compile with /E to see that the last line truly is preprocessed to

#pragma message ("This surely ought not work")

Copyright © 2005-2006. Geoff Chappell. All rights reserved.

Please consider that this intrusive sentence could instead be proclaiming that you help sponsor this research.

Home > Programming > Tools > Visual C++ > Language > Preprocessor

[Home][Programming Samples][Application Notes][Security Notes][Editorial][Consultation][Contacts]