当前位置:网站首页>C language preprocessing instruction
C language preprocessing instruction
2022-07-27 19:26:00 【BasilGuo】
C Language preprocessing instructions
0. List
| Instructions | explain |
|---|---|
| #include | Contains a file |
| #define | Defining macro |
| #undef | Cancel defined macro |
| #ifdef | If the macro has been defined , Then compile the following code |
| #ifndef | If the macro is not defined , Then compile the following code |
| #endif | End one #if……#else Conditional compilation block |
| #if | If the given condition is true , Then compile the following code |
| #elif | If the front #if Given conditions are not true , The current condition is true , Then compile the following code |
| #else | coordination #if Use |
| # | Stringize operator, Is to string macro parameters |
| ## | Token pasting |
| #pragma | The most complicated , There is no way to elaborate here |
| #error | Report an error and stop precompiling |
| #warning | Warn and continue precompiling |
1. brief introduction
C The language preprocessing instruction is a declaration ( Text ) Substitution expression , It uses # start , And it must be the first character ,# Next is the instruction keyword , In keywords and # Any number of white space characters are allowed between , The entire line of statements constitutes a processing instruction , This instruction performs some conversion of the source code before the compiler compiles .
C Preprocessing instructions are not C Part of the compiler , It's different from the standard C There are some grammatical differences , for example :
- It uses
#start . - It is based on the behavior unit , Each statement has a separate line , But standard C It's not compulsory .
- It ends with a new behavior , But the standard C To use
;ending .
2. Instructions
2.1 #include
Used to import another file , Mainly header files . There are two ways
#include <file.h>: from C Find the header file in the standard library of#include "path_to_header_file": Look in the current directory , Then go again. C Find in the standard library , Mainly customized header files
2.2 #define
Macro definition , Given value / Constant / Expression defines a name , Format #define MACRO_NAME macro_body
among MACRO_NAME yes C identifier , and macro_body Is string / value / expression .
give an example :
#include <stdio.h>
// MACRO definition
#define COUNTRY "CHINA" // String constant
#define TRUE 1 // integer constants
#define FALSE 0 // integer constants
#define SUM(x,y) ((x)+(y)) // Macro definition
int main(void)
{
printf("COUNTRY: %s\n", COUNTRY);
printf("TRUE: %d\n", TRUE);
printf("FALSE: %d\n", FALSE);
printf("SUM(10+20): %d\n", SUM(10, 20));
return 0;
}
result :
COUNTRY: CHINA
TRUE: 1
FALSE: 0
SUM(10+20): 30
2.3 #undef
and #define The positive sign is opposite , Used to remove macro definitions , If it's already defined , The remove , If there is no definition , It doesn 't matter . Format :#undef MACRO_NAME
give an example :
#include <stdio.h>
// MARCO definition
#define TRUE 1
#define FALSE 0
int main(void)
{
printf("TRUE: %d\n", TRUE);
printf("FALSE: %d\n", FALSE);
// Undefine a previously defined macro
#undef TRUE
#undef FALSE
// Re-define macro values
#define TRUE 0
#define FALSE 1
printf("\nMacro values are redefinition\n");
printf("TRUE: %d\n", TRUE);
printf("FALSE: %d\n", FALSE);
return 0;
}
result
TRUE: 1
FALSE: 0
Macro values are redefinition
TRUE: 0
FALSE: 1
If in #undef After use, an error will be reported :
ex1.c: In function 'main':
ex1.c:14:26: error: 'TRUE' undeclared (first use in this function)
printf("TRUE: %d\n", TRUE);
^~~~
ex1.c:14:26: note: each undeclared identifier is reported only once for each function it appears in
ex1.c:15:27: error: 'FALSE' undeclared (first use in this function); did you mean 'FILE'?
printf("FALSE: %d\n", FALSE);
^~~~~
FILE
2.4 #ifdef, #ifndef, #endif
This is a preprocessing instruction with conditions , Used to check whether macros are defined . among #ifdef and #ifndef It's complementary .
#ifdef: If macros have been defined , Then compile the following code , Format :#ifdef MACRO_NAME#ifndef: If no macro is defined , Then compile the following code , Format :#ifndef MACRO_NAME#endif: End the above code , Matching use of , One for one , There must be
give an example :
#include <stdio.h>
// MACRO definition
#define COUNTRY "CHINA"
int main(void)
{
#ifdef COUNTRY
printf("Country is defined\n");
#endif
#ifndef PROVINCE
printf("PROVINCE is not defined. Defining PROVINCE... \n");
#define PROVINCE "HN"
#endif
printf("Province is: %s\n", PROVINCE);
return 0;
}
result
Country is defined
PROVINCE is not defined. Defining PROVINCE...
Province is: HN
The main purpose of these instructions is to prevent a header file from being loaded repeatedly many times . For example, there is a custom header file myfile.h, In multiple files *.c Use... In the document , In order to prevent multiple loading , This file can be defined as follows
#ifndef _MY_FILE_H
#define _MY_FILE_H
// Declare other macros
// Declare variables and methods
#endif
At the first load ,C The preprocessor will check that there is no definition _MY_FILE_H, Will execute the definition , And declare other macros 、 Variables and methods , When loading and importing for the second time , It's already defined _MY_FILE_H, There will be no restatement .
2.5 #if, #elif, #else and #endif
These instructions are similar C In language if...else... sentence , For conditional compilation . Format :
#if expression
// If condition is true
#elif expression
// If else if condition is true
#else
// If no condition is true
#endif
give an example :
#include <stdio.h>
#define IND 1
#define USA 2
#define UK 3
#define COUNTRY IND
int main()
{
#if COUNTRY == IND
printf("Selected country code is: %d\n", COUNTRY);
// Do some task if country is India
#elif COUNTRY == USA
printf("Selected country code is: %d\n", COUNTRY);
// Do some task if country is USA
#else
printf("Selected country code is: %d\n", COUNTRY);
// Do some task if country is UK
#endif
return 0;
}
2.6 #,##
These are two special C Preprocessing instruction , Is used to process strings .
#, Used to convert macro parameters into strings , Make it unnecessary to use double quotation marks to enclose the display , Format :#define MACRO_NAME(param) #param, give an example :
#include <stdio.h>
#define PRINT(msg) #msg
int main(void)
{
printf(PRINT(I love programming));
return 0;
}
result :
I love programming
##, Used to connect two data , Put them together as strings . Format :```#define MACTRO_NAME(param1, param2) param1##param2, give an example :
#include <stdio.h>
#define CONCAT(a,b) a##b
int main(void)
{
printf("CONCAT(10, 20) = %d\n", CONCAT(10, 20));
return 0;
}
result :
CONCAT(10, 20) = 1020
2.7 #pragma
In all preprocessing instructions ,#pragma Instructions are probably the most complex , Its function is to set the state of the compiler or instruct the compiler to complete some specific actions .#pragma Instructions give a method to each compiler , In keeping with C and C++ When the language is fully compatible , Give host or operating system specific features . By definition , Compilation instructions are machine or operating system specific , And it's different for each compiler .
Format :#pragma Para
2.7.1 message
message Parameters can output corresponding information in the compilation information output window , This is very important for the control of source code information . Format :#pragma message("Text"). When the compiler encounters this instruction, it prints out the message text in the compile output window . give an example :
#ifdef _X86
#pragma message("_X86 macro activated!")
#endif
2.7.2 code_seg
Format :#pragma code_seg(["section-name"[, "section-class"]])
It can set the code segment stored in the function code in the program , It will be used when developing drivers .
2.7.3 once
This is more commonly used , It is mainly used to add this instruction at the beginning of the header file to ensure that the header file is imported and compiled only once . and #ifndef That's similar to , But this has compatibility problems , Some compilers do not support , Some already supported are also considering whether to continue to support .
The rest , When I use , Add more specific explanations . You can also refer to Baidu Encyclopedia 了 .
2.7.4 hdrstop
2.7.5 resource
2.7.6 warning
2.7.7 comment
2.7.8 disable
2.7.9 data_seg
2.7.10 region
3. quote
边栏推荐
- HDU1573 X问题【一元线性同余方程组】
- kettle EXCEL 累计输出数据
- Kinect for Unity3d----KinectManager
- How to generate random numbers with standard distribution or Gaussian distribution
- 阿里云对象存储OSS的开通和使用
- 每日一题(02):倒置字符串
- How to break the team with automated testing
- 专项测试之「 性能测试」总结
- Jmeter接口自动化-如何解决请求头Content-Type冲突问题
- kettle JVM内存设置---效果不明显
猜你喜欢

Unity display Kinect depth data

阿里云视频点播服务的开通和使用

编程式跳转

IDEA连接数据库时区问题,报红Server returns invalid timezone. Need to set ‘serverTimezone‘ property.

Webmagic+selenium+chromedriver+jdbc垂直抓取数据。

sql 字段类型转换

Self control principle learning notes - system stability analysis (2) - loop analysis and Nyquist bode criterion

Kinect for Unity3d----KinectManager

C语言案例:密码设置及登录> 明解getchar与scanf

v-if,v-else,v-for
随机推荐
Usage of ref keyword
Self control principle learning notes - system stability analysis (2) - loop analysis and Nyquist bode criterion
图的遍历的定义以及深度优先搜索和广度优先搜索(二)
kettle入门级操作第一篇(读取excel、输出excel)
MySQL learning notes (2) -- stored procedures and stored functions
又有一个Repeater的例子
每日一题(02):倒置字符串
The understanding of string in C.
Latex use - control the display position of tables or graphics
收下这份实操案例,还怕不会用Jmeter接口测试工具
Kinect for Unity3d----KinectManager
ref 关键字的用法
MySQL学习笔记(2)——存储过程与存储函数
WinForm screenshot save C code
mysql学习笔记(1)——变量
2022 preparation for autumn recruitment 10W word interview sketch PDF version, with operating system and computer network interview questions
编程式跳转
Opening and using Alibaba cloud object storage OSS
换行问题双保险
webservice的疑问