"C Language Practice every day (ii)" pretreatment

Source: Internet
Author: User
Tags define definition

Introduction:


Learn C language at the beginning. When it comes to preprocessing, the brain is thinking of the definition of a # define macro and the header files included with # include. Later, with the deep study of C found. Preprocessing is more than that. For example, conditional compilation, pre-defined macros, and so on. This is summarized below.

The pre-processing definition is given: before compiling the program, the preprocessor examines the program (hence called the preprocessor) and replaces the abbreviations in the program with the content represented by the symbol abbreviations, based on the preprocessor commands used in the program.


1. #define

The most frequently used preprocessor command is the Define command, which consists of three parts: #define本身, symbol abbreviations, alternate lists (or principals).

Structure can be written as: #define macro body of the macro part can not have spaces, can only be numbers, underscores, letters. But the first character cannot make a number.

such as: #define PX printf ("X is%d.\n", X)

After the preprocessor discovers a macro in the program, it replaces the macro with its equivalent replacement file. Assume that the string also contains macros. Then continue to replace these macros. Assuming that the macro is present in the double-argument, no substitution is made.

Suppose the macro that appears in the double-argument has a "#运算符" in front of it, you can create a string with a macro argument. Such as:

#define PSQR (x) printf ("The square of X is%d.\n", ((x) * (x)))

PSQR (8)//is equivalent to printf ("The square of X is%d.\n", ((8) * (8))). Output the square of X is 64.

#define PSQR (x) printf ("The Square of" #X "is%d.\n", ((x) * (x)))

PSQR (8)//is equivalent to printf ("The squareof 8 is%d.\n", ((8) * (8))) and outputs the square of 8 is .

Summary: The x in the character in the argument is treated as plain text, not as a replaceable language symbol. #符号用作一个预处理运算符, it can convert language symbols into strings.


Pre-processor Adhesives: # #运算符. Same as the # operator above. # #运算符能够用于类函数宏的替换部分.

# #还能用于类对象宏的替换部分, combine two symbols into a single language symbol, such as:

#define XNAME (n) x# #n

XNAME (4)//equivalent to X4


As can be seen from the definition and use of macros, it is similar to the function, but the macros and functions are different, their choice is actually the tradeoff between time and space.

Macros produce inline code, which means that statements are generated in the program.

Assuming that you use a macro 20 times, you will insert 20 lines of code into your program. Assuming that the function is used 20 times, there is only one copy of the function statement in the program, thus saving space.

There is one more aspect. The control of the program must be transferred to the function and then returned to the calling program. So this is more time than the inline code charges.


2. #include

After the preprocessor discovers the # include directive, it looks for the file name followed by and includes the contents of the file in the current file. The text that is included in the file will replace the # include directive in the source file. It's like typing all the contents of the protected file into this particular location in the source file.

#include <name.h>//file in angle brackets, search system working folder

#include "name.h"//File name in double-cited, search current working folder

#include "/dir1/dir2/name.h"//Search/dir1/dir2 folder

The general contents of the header file include: Obvious constants, macro functions, function declarations, struct template definitions, type definitions


3. Other Directives

#undef指令取消前面的 a # define definition.

#if #ifdef #ifndef #else #elif #endif指令可用于选择什么情况下编译哪些代码. #line指令用于重置行和文件信息, #error指令用于给出错误消息. #param指令用于想编译器发出指示


4. Pre-defined macros:

__DATE__: Date of preprocessing

__FILE__: string literal for code current source file name

__LINE__: integer constant representing the line number in the current source file

__STDC__: Set to 1 o'clock. Indicates that the implementation follows the C standard

__TIME__: Source file compilation time

"C Language Practice every day (ii)" pretreatment

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.