Are C preprocessor statements a part of the C language?

北战南征 提交于 2019-12-12 11:54:43

问题


I recall a claim made by one of my professors in an introductory C course. He stated that the #define preprocessor command enables a programmer to create a constant for use in later code, and that the command was a part of the C language.

/* Is this truly C code? */
#define FOO 42

Since this was in an introductory programming class, I suspect that he was merely simplifying the relationship between the source file and the compiler, but nevertheless I wish to verify my understanding.

Are preprocessor statements completely independent from the C language (dependent on the specific compiler used) or are they explicitly described in the C99 standard? Out of curiosity, did K&R ever mention preprocessor macros?


回答1:


Yes, the standard describes the preprocessor. It's a standardized part of the C language.

Note that #include, which is essential for modularization of code, is a preprocessor directive.

In the publically-available draft of the C99 standard, the preprocessor is described in section 6.10.




回答2:


The preprocessor is indeed part of the C and C++ standard (chapter 16 in the C++ standard) and the standards describe how the preprocessor and the language interact (for example it is illegal to re-#define the C keywords).

However the C preprocessor can work with other languages than C for any kind of simple file preprocessing (I have seen it used with LaTeX files for example).




回答3:


Yes the preprocessor is part of the C language. Conceptually it is ran before the source is compiled.

Along with constant definitions, the preprocessor is used to implement two very important constructs:

  1. #include which brings other files into the compilation unit.

  2. include guards; i.e. the pattern,

    #if !defined(METAWORD)
    #define METAWORD 1
    /* struct definition, function prototype */
    #endif
    

Out of interest, these two usages have survived into C++, constant definition can be implemented in other (better?) ways.



来源:https://stackoverflow.com/questions/17149001/are-c-preprocessor-statements-a-part-of-the-c-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!