Study Notes--C Language Preprocessing--Conditional Compilation

Publisher:csZhouLatest update time:2015-09-29 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Preprocessor name meaning

#define macro definition 

#undef cancels the defined macro name

#include causes the compiler to embed another source file into the source file with #include  

#if   #else   #elif #endif and other conditional compilation

#line changes the current line number and file name, which are predefined identifiers in the compiler.

The basic form of the command is as follows: #line number["filename"]

#error When compiling a program, as soon as #error is encountered, a compilation error message will be generated and compilation will stop.

#pragma   is a command defined at implementation time that allows various instructions to be sent to the compiler. For example, the compiler may have an option that supports tracing of program execution. A tracing option can be specified using the #pragma statement.

Conditional compilation
The conditional compilation function allows us to compile different parts of a program according to different conditions, thereby generating different target code files. This is very useful for program porting and debugging. There are three forms of conditional compilation, which are introduced below.

The first form:
#ifdef identifier
Segment 1
#else
Segment 2
#endif

Its function is to compile program segment 1 if the identifier has been defined by the #define command; otherwise, compile program segment 2. If there is no program segment 2 (it is empty), the #else in this format can be omitted, that is, it can be written as:
#ifdef identifier
program segment
#endif
The second form:
#ifndef identifier
program segment 1
#else
program segment 2
#endif
is different from the first form in that "ifdef" is changed to "ifndef". Its function is to compile program segment 1 if the identifier has not been defined by the #define command, otherwise, compile program segment 2. This is the opposite of the function of the first form.

The third form:
#if constant expression
Program segment 1
#else
program segment 2
#endif
Its function is to compile program segment 1 if the value of the constant expression is true (non-0), otherwise compile program segment 2. Therefore, the program can perform different functions under different conditions.
As for the #elif command, its meaning is the same as else if. It forms an if else-if ladder statement, which can make multiple compilation options.

Reference address:Study Notes--C Language Preprocessing--Conditional Compilation

Previous article:STM8s--Timer (time4)
Next article:extern and header files

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号