C Language Learning Notes (v) Preprocessing symbols

Source: Internet
Author: User

"Common sense" with regard to preprocessing

    • Preprocessing is not a compilation, nor is it "precompiled."

    • Preprocessing is not available in every language.

    • The pre-and C + + preprocessing simply divides the source program into segments (phase) and does not compile.

    • The preprocessor is typically abbreviated as PP in Unix tradition, and in an automated build script the C preprocessor is abbreviated as a macro reference for CPP. In order not to cause ambiguity, C + + (C-plus-plus) is often not abbreviated to CPP, but to cxx.


C Language Preprocessing symbols

The ANSI C standard requires that the supported preprocessing symbols include:

#define, #undef (macro definition), #include(file contains), #if, #else, #elif, #endif, #ifdef, #ifndef(conditional compilation), #line(Row control), #error (Error handling), #pragma(Implementation related), #(escaped), ##(parameter connection).


Considerations for macro Definitions

trying to use a macro to define a comment symbol is not possible. , such as the following code:

#define BSC//#define BMC/* #define EMC */BSC my single-line comment bmc my multi-line comment EMC


Because annotations precede preprocessing directives, a heap of errors naturally occur when you expand these macro definitions.

The macro must not be stingy when defining an expression . This doesn't have to be explained.

macro name should not have a space when the macro is defined . The following definitions can cause a lot of trouble:

#define SUM (x) ((x) * (x))

Once you have used the #undef undo macro, the following code cannot use the macro unless it is defined again. In addition, if no #undef is defined directly, then the definition overrides the previous definition . The C value in the following code is 4:

#include <stdio.h> #define x 3 #define y x*2 #undef x #define x 2 int c = y;        int main (int argc, char** argv) {printf ("%d", c);    return 0; }

Macros are expanded only when they are in use, otherwise there is no compilation error even if there is a problem with the definition. If the second # define in the code above is commented out and the value of C is assigned to 0, the x definition is revoked, and no error is given, because Y is not used and is not expanded.


#pragma的常用参数

message : Used to display the corresponding information in the output information of the compilation window. The following is a typical application:

#ifdef _x86 #pragma message ("x86 macro is activated."); #endif

code_seg : It is used when developing the driver.

once: Specifies (ensures) that the file is compiled only once (typically a header file).

hdrstop : Indicates that the following header file is not compiled.

Pack : Specifies the memory alignment parameters.


#符号和 # #符号

#符号用于转义. That's not a good idea to understand, see examples:

#define PRTSQR (x) printf ("The Sqr of X is%d.\n", ((x) * (x)));

If PRTSQR (4) is executed, the output of this code is:

The SQR of X is 16.

If you want to display the value of a parameter, precede the character x in the string with a # and add a quotation mark, i.e.:

#define PRTSQR (x) printf ("The Sqr of" #x "is%d.\n", ((x) * (x)));

The output is as follows:

The SQR of 4 is 16.


# #运算符用于连接 , such as the following macro:

#define XNAME (n) x# #n int XNAME (8);

The second line of code above will be expanded to X8.


The preprocessing symbols are summarized here first.

This article is from the "Flying Cat" blog, be sure to keep this source http://flyingcat2013.blog.51cto.com/7061638/1570411

C Language Learning Notes (v) Preprocessing symbols

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.