9. Pre-processing command of C language

Source: Internet
Author: User

preprocessing commands

  Basic Concepts: The ANSI C standard stipulates that some "preprocessing commands" can be added to the C source program in order to improve the programming environment and improve the efficiency.

These preprocessing commands are specified by ANSI C, but they are not part of the C language itself and cannot be compiled directly (because the compiler does not recognize them). These special commands in the program must be "precompiled" prior to the usual compilation of the program

After preprocessing, the program can be compiled by the compiler to the pre-processing of the source program to do the usual compilation, to get the target code for execution.

One important difference between C and other high-level languages is that you can use preprocessing commands and features that have preprocessing.

The following three types of preprocessing functions are available in C:

1. Macro definition

2. The file contains

3. Conditional compilation

These features are implemented using macro-definition commands, file-containing commands, and conditional-compilation commands. To be distinguished from the general C language, these commands begin with the symbol "#". For example: #define #include

1. Macro definition

Macro definition with no parameters

Macro defines the general form:

#define Identifier string

#define PI 3.1415926

The macro definition uses the specified identifier PI in this program file instead of the string "3.1415926", and in the case of compile preprocessing, all the pi that appears in the program after the command is replaced with "3.1415926" . This method allows the user to replace a long string with a simple name.

This identifier (name) is called the "macro name"

The process of replacing a macro name with a string at precompilation is called a macro expansion. #define是宏定义命令

Description

(1) Macro names are generally used in uppercase letters to distinguish them from variable names. This is not a rule, but it can also be used in lowercase letters

(2) Use a macro name instead of a string to reduce the amount of repetitive writing of certain strings in a program

    (3) macro definition with a macro name instead of a string, only do simple substitution, do not check the correctness, only after compiling the macro expansion of the source program will be found syntax errors and error.

(4) The macro definition is not a C statement, and you do not need to add semicolons at the end. If a semicolon is added, it will be replaced with a semicolon.

(5) #define命令出现在程序中函数的外面, the valid range of the macro name is defined after the command to the end of the source file. Typically, #define命令写在文件开头, the function is valid as part of the file before it is in scope.

(6) The scope of the macro definition can be terminated with the #undef command

(7) In the macro definition, you can reference the defined macro name, you can layer displacement

For example:

1 #include <stdio.h>2#define R 3.03#define PI 3.14159264 #define L 2*pi*r5#define S pi*r*r6void  main ()7{ 8     printf ("L =%f \ n S =%f", l,s); 9 }

Macro definition with Parameters

Action: Not a simple string substitution, but also a parameter substitution

Macro definitions with parameters in general form are:

#define Macro Name (parameter table) string

The parameters specified in the parentheses are contained in the string

such as: #define S (A, b) a*b

2, the file contains processing

Function: One source file can contain the contents of another source file in all

General form: #include "filename" //Search in the current directory, in the search criteria directory

#include < file name > //Search directly by standard directory

Processing: When precompiled, replace the pre-processing command with the contents of the contained file, and then compile the "included" file as a source file unit to get the target file. obj

Contents of the included file:

source file (*.C)

Header file (*.h)//macro definition, data structure definition, function description, etc.

file contains nested

Precompiled becomes a file, so the global static variable in file2.c is valid in file1.c and does not have to be declared with extern

3. Conditional compilation

Concept: The so-called "conditional compilation", when a part of the content specified compilation conditions, so that it only to meet certain conditions to compile

Several forms of conditional compilation commands:

(1) #ifdef identifier (2) #ifndef identifier (3) #if expression

Procedure Section 1 program Segment 1 program segment 1

#else #esle #else

Procedure Section 2 program Segment 2 program segment 2

#endif #endif #endif

9. Pre-processing command of C language

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.