📄 cpp.texi
字号:
Replacement: [ ] @{ @} # \ ^ | ~@end smallexample@item@cindex continued lines@cindex backslash-newlineContinued lines are merged into one long line.A continued line is a line which ends with a backslash, @samp{\}. Thebackslash is removed and the following line is joined with the currentone. No space is inserted, so you may split a line anywhere, even inthe middle of a word. (It is generally more readable to split linesonly at white space.)The trailing backslash on a continued line is commonly referred to as a@dfn{backslash-newline}.If there is white space between a backslash and the end of a line, thatis still a continued line. However, as this is usually the result of anediting mistake, and many compilers will not accept it as a continuedline, GCC will warn you about it.@item@cindex comments@cindex line comments@cindex block commentsAll comments are replaced with single spaces.There are two kinds of comments. @dfn{Block comments} begin with@samp{/*} and continue until the next @samp{*/}. Block comments do notnest:@smallexample/* @r{this is} /* @r{one comment} */ @r{text outside comment}@end smallexample@dfn{Line comments} begin with @samp{//} and continue to the end of thecurrent line. Line comments do not nest either, but it does not matter,because they would end in the same place anyway.@smallexample// @r{this is} // @r{one comment}@r{text outside comment}@end smallexample@end enumerateIt is safe to put line comments inside block comments, or vice versa.@smallexample@group/* @r{block comment} // @r{contains line comment} @r{yet more comment} */ @r{outside comment}// @r{line comment} /* @r{contains block comment} */@end group@end smallexampleBut beware of commenting out one end of a block comment with a linecomment.@smallexample@group // @r{l.c.} /* @r{block comment begins} @r{oops! this isn't a comment anymore} */@end group@end smallexampleComments are not recognized within string literals.@t{@w{"/* blah */"}} is the string constant @samp{@w{/* blah */}}, notan empty string.Line comments are not in the 1989 edition of the C standard, but theyare recognized by GCC as an extension. In C++ and in the 1999 editionof the C standard, they are an official part of the language.Since these transformations happen before all other processing, you cansplit a line mechanically with backslash-newline anywhere. You cancomment out the end of a line. You can continue a line comment onto thenext line with backslash-newline. You can even split @samp{/*},@samp{*/}, and @samp{//} onto multiple lines with backslash-newline.For example:@smallexample@group/\**/ # /**/ defi\ne FO\O 10\20@end group@end smallexample@noindentis equivalent to @code{@w{#define FOO 1020}}. All these tricks areextremely confusing and should not be used in code intended to bereadable.There is no way to prevent a backslash at the end of a line from beinginterpreted as a backslash-newline. This cannot affect any correctprogram, however.@node Tokenization@section Tokenization@cindex tokens@cindex preprocessing tokensAfter the textual transformations are finished, the input file isconverted into a sequence of @dfn{preprocessing tokens}. These mostlycorrespond to the syntactic tokens used by the C compiler, but there area few differences. White space separates tokens; it is not itself atoken of any kind. Tokens do not have to be separated by white space,but it is often necessary to avoid ambiguities.When faced with a sequence of characters that has more than one possibletokenization, the preprocessor is greedy. It always makes each token,starting from the left, as big as possible before moving on to the nexttoken. For instance, @code{a+++++b} is interpreted as@code{@w{a ++ ++ + b}}, not as @code{@w{a ++ + ++ b}}, even though thelatter tokenization could be part of a valid C program and the formercould not.Once the input file is broken into tokens, the token boundaries neverchange, except when the @samp{##} preprocessing operator is used to pastetokens together. @xref{Concatenation}. For example,@smallexample@group#define foo() barfoo()baz @expansion{} bar baz@emph{not} @expansion{} barbaz@end group@end smallexampleThe compiler does not re-tokenize the preprocessor's output. Eachpreprocessing token becomes one compiler token.@cindex identifiersPreprocessing tokens fall into five broad classes: identifiers,preprocessing numbers, string literals, punctuators, and other. An@dfn{identifier} is the same as an identifier in C: any sequence ofletters, digits, or underscores, which begins with a letter orunderscore. Keywords of C have no significance to the preprocessor;they are ordinary identifiers. You can define a macro whose name is akeyword, for instance. The only identifier which can be considered apreprocessing keyword is @code{defined}. @xref{Defined}.This is mostly true of other languages which use the C preprocessor.However, a few of the keywords of C++ are significant even in thepreprocessor. @xref{C++ Named Operators}.In the 1999 C standard, identifiers may contain letters which are notpart of the ``basic source character set'', at the implementation'sdiscretion (such as accented Latin letters, Greek letters, or Chineseideograms). This may be done with an extended character set, or the@samp{\u} and @samp{\U} escape sequences. The implementation of thisfeature in GCC is experimental; such characters are only accepted inthe @samp{\u} and @samp{\U} forms and only if@option{-fextended-identifiers} is used.As an extension, GCC treats @samp{$} as a letter. This is forcompatibility with some systems, such as VMS, where @samp{$} is commonlyused in system-defined function and object names. @samp{$} is not aletter in strictly conforming mode, or if you specify the @option{-$}option. @xref{Invocation}.@cindex numbers@cindex preprocessing numbersA @dfn{preprocessing number} has a rather bizarre definition. Thecategory includes all the normal integer and floating point constantsone expects of C, but also a number of other things one might notinitially recognize as a number. Formally, preprocessing numbers beginwith an optional period, a required decimal digit, and then continuewith any sequence of letters, digits, underscores, periods, andexponents. Exponents are the two-character sequences @samp{e+},@samp{e-}, @samp{E+}, @samp{E-}, @samp{p+}, @samp{p-}, @samp{P+}, and@samp{P-}. (The exponents that begin with @samp{p} or @samp{P} are newto C99. They are used for hexadecimal floating-point constants.)The purpose of this unusual definition is to isolate the preprocessorfrom the full complexity of numeric constants. It does not have todistinguish between lexically valid and invalid floating-point numbers,which is complicated. The definition also permits you to split anidentifier at any position and get exactly two tokens, which can then bepasted back together with the @samp{##} operator.It's possible for preprocessing numbers to cause programs to bemisinterpreted. For example, @code{0xE+12} is a preprocessing numberwhich does not translate to any valid numeric constant, therefore asyntax error. It does not mean @code{@w{0xE + 12}}, which is what youmight have intended.@cindex string literals@cindex string constants@cindex character constants@cindex header file names@c the @: prevents makeinfo from turning '' into ".@dfn{String literals} are string constants, character constants, andheader file names (the argument of @samp{#include}).@footnote{The Cstandard uses the term @dfn{string literal} to refer only to what we arecalling @dfn{string constants}.} String constants and characterconstants are straightforward: @t{"@dots{}"} or @t{'@dots{}'}. Ineither case embedded quotes should be escaped with a backslash:@t{'\'@:'} is the character constant for @samp{'}. There is no limit onthe length of a character constant, but the value of a characterconstant that contains more than one character isimplementation-defined. @xref{Implementation Details}.Header file names either look like string constants, @t{"@dots{}"}, or arewritten with angle brackets instead, @t{<@dots{}>}. In either case,backslash is an ordinary character. There is no way to escape theclosing quote or angle bracket. The preprocessor looks for the headerfile in different places depending on which form you use. @xref{IncludeOperation}.No string literal may extend past the end of a line. Older versionsof GCC accepted multi-line string constants. You may use continuedlines instead, or string constant concatenation. @xref{Differencesfrom previous versions}.@cindex punctuators@cindex digraphs@cindex alternative tokens@dfn{Punctuators} are all the usual bits of punctuation which aremeaningful to C and C++. All but three of the punctuation characters inASCII are C punctuators. The exceptions are @samp{@@}, @samp{$}, and@samp{`}. In addition, all the two- and three-character operators arepunctuators. There are also six @dfn{digraphs}, which the C++ standardcalls @dfn{alternative tokens}, which are merely alternate ways to spellother punctuators. This is a second attempt to work around missingpunctuation in obsolete systems. It has no negative side effects,unlike trigraphs, but does not cover as much ground. The digraphs andtheir corresponding normal punctuators are:@smallexampleDigraph: <% %> <: :> %: %:%:Punctuator: @{ @} [ ] # ##@end smallexample@cindex other tokensAny other single character is considered ``other''. It is passed on tothe preprocessor's output unmolested. The C compiler will almostcertainly reject source code containing ``other'' tokens. In ASCII, theonly other characters are @samp{@@}, @samp{$}, @samp{`}, and controlcharacters other than NUL (all bits zero). (Note that @samp{$} isnormally considered a letter.) All characters with the high bit set(numeric range 0x7F--0xFF) are also ``other'' in the presentimplementation. This will change when proper support for internationalcharacter sets is added to GCC@.NUL is a special case because of the high probability that itsappearance is accidental, and because it may be invisible to the user(many terminals do not display NUL at all). Within comments, NULs aresilently ignored, just as any other character would be. In runningtext, NUL is considered white space. For example, these two directiveshave the same meaning.@smallexample#define X^@@1#define X 1@end smallexample@noindent(where @samp{^@@} is ASCII NUL)@. Within string or character constants,NULs are preserved. In the latter two cases the preprocessor emits awarning message.@node The preprocessing language@section The preprocessing language@cindex directives@cindex preprocessing directives@cindex directive line@cindex directive nameAfter tokenization, the stream of tokens may simply be passed straightto the compiler's parser. However, if it contains any operations in the@dfn{preprocessing language}, it will be transformed first. This stagecorresponds roughly to the standard's ``translation phase 4'' and iswhat most people think of as the preprocessor's job.The preprocessing language consists of @dfn{directives} to be executedand @dfn{macros} to be expanded. Its primary capabilities are:@itemize @bullet@itemInclusion of header files. These are files of declarations that can besubstituted into your program.@itemMacro expansion. You can define @dfn{macros}, which are abbreviationsfor arbitrary fragments of C code. The preprocessor will replace themacros with their definitions throughout the program. Some macros areautomatically defined for you.@itemConditional compilation. You can include or exclude parts of theprogram according to various conditions.@itemLine control. If you use a program to combine or rearrange source filesinto an intermediate file which is then compiled, you can use linecontrol to inform the compiler where each source line originally camefrom.@itemDiagnostics. You can detect problems at compile time and issue errorsor warnings.@end itemizeThere are a few more, less useful, features.Except for expansion of predefined macros, all these operations aretriggered with @dfn{preprocessing directives}. Preprocessing directivesare lines in your program that start with @samp{#}. Whitespace isallowed before and after the @samp{#}. The @samp{#} is followed by anidentifier, the @dfn{directive name}. It specifies the operation toperform. Directives are commonly referred to as @samp{#@var{name}}where @var{name} is the directive name. For example, @samp{#define} isthe directive that defines a macro.The @samp{#} which begins a directive cannot come from a macroexpansion. Also, the directive name is not macro expanded. Thus, if@code{foo} is defined as a macro expanding to @code{define}, that doesnot make @samp{#foo} a valid preprocessing directive.The set of valid directive names is fixed. Programs cannot define newpreprocessing directives.Some directives require arguments; these make up the rest of thedirective line and must be separated from the directive name bywhitespace. For example, @samp{#define} must be followed by a macroname and the intended expansion of the macro.A preprocessing directive cannot cover more than one line. The linemay, however, be continued with backslash-newline, or by a block commentwhich extends past the end of the line. In either case, when thedirective is processed, the continuations have already been merged withthe first line to make one long line.@node Header Files@chapter Header Files
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -