Abstract
Because I need to use C language programming in my work and study, I often encounter[^1] the situation[^2] that I have a little understanding of some grammar. So I have the idea of studying C language syntax[^3] from the beginning. This chapter[^4] will study the preprocessing command part of the C language. Starting from the meaning of the command, the use of examples to complete the whole process[^5] of learning.
Thanks
Thanks to en.cppreference.com for contributing[^6] to the C manual.
Running environment[^7]
environment[^7]
- Windows 11 Pro Version: 22H2.
- Visual Studio Code Version: 1.74.2 with C/C++ extensions[^8].
Let's start with the simplest program
Let’s write a basic c program:
1 |
|
The output from this program is:
1 | Hello, World! |
#include Insert the contents of another file
Insert[^9],contents[^10]
First let’s look at the meaning of the first line, This line of code has two parts: #include and <stdio.h>.
This command takes two forms:
1 |
Using <> Is searched[^11] for filename in the system standard library
Using “” means searching for filename in the current directory
#include is one of several C preprocessing commands. Preprocess commands are commands that are used before the program is executed.
All preprocessing commands in C are as follows:
- #define
- #include
- #,##
- #error
- #if, #ifdef, #ifndef, #else, #elif, #endif
- #line
- #pragma
- #undef
#define Defining variables
Syntax:
#define macro-name replacement[^12]-string
The #define command is used to replace[^13] the replacement-string with macro-name . In other words, #define causes the compiler to define each of the replacement-string with a new macro-name . The substituted[^14] string terminates[^15] at the end of the line.
For example:
1 |
These two statements mean give 1 a new name: TRUE ; and give 0 a new name: FALSE .
Later in the program, you can just use TRUE and FALSE instead of 1 and 0 .
Another thing the #define command does is replace, make it look like have created a function.
For example:
1 |
It does the same thing as defining a subfunction:
1 | int Compare_sizes (int a, int b) |
It is used in the same way:
1 | //#define |
These two statements mean that a is compared to b, which outputs TRUE if a is greater[^16] than b and FALSE if a is less than b.
Study English Reference By my self
[^1]: encounter /ɪnˈkaʊntə®/
v. 遭遇;偶遇,邂逅
n. 偶遇,邂逅;经历,体验;冲突;比赛,交锋
[^2]: situation /ˌsɪtʃuˈeɪʃ(ə)n/
n. 情况,形势;重要问题,突发情况;(建筑物或城镇的)地理位置,环境特点;<旧,正式>工作,职位
[^3]: syntax /ˈsɪntæks/
n. 句法,句法规则;(计算机语言的)句法,语构;句子结构分析法;句法学
[^4]: chapter /ˈtʃæptə®/
n. 章,回,篇;阶段,时期;分会,分部;议会法案;一系列,一连串(a chapter of);全体教士;宗教团体理事会
v. 把……分成章节
[^5]: process /ˈprəʊses/
n. 步骤,程序;(自然或偶然的)变化过程;(为达到某目标的)过程,进程;制作方法,加工方法;<法律>传票;(生,剖)端突,突起
adj. (印刷)三原色的,三色版的; 经过特殊加工的;照相板的
v. (用化学物品或机器)处理,加工;审核,受理(正式文件或请求);(计算机)处理(数据);冲洗(照片);加工(食品);<正式>列队行进;把(头发)弄成直发
[^6]: contributing /ˈkɒntrɪbjuːtɪŋ/
adj. 贡献的;起作用的
v. 捐献(contribute 的 ing 形式)
[^7]: environment /ɪnˈvaɪrənmənt/
n. 自然环境,生态环境;周围状况,条件;工作平台,软件包
[^8]: extension /ɪkˈstenʃ(ə)n/
n. 延伸,扩展;展期,延长期;扩建部分,延伸部分;(电话)分机;扩展名;(为非全日制学生开设的)进修部;牵伸(术);外延;广延(性)
[^9]: Insert /ɪnˈsɜːt/
v. 插入,嵌入;(在文章中)添加,加插;使参与(活动);(肌肉或其他器官)附着于(尤指移动部分)
n. 插入物;(尤指)插页,附加页;(电影等的)插入镜头
[^10]: contents /ˈkɒntents/
n. 内容;[图情] [计] 目录;要旨(content 的复数)
v. 使满意(content 的三单形式)
[^11]: search /sɜːtʃ/
v. 搜查,搜寻;搜查(地方,车辆),搜……的身;思索,细想(问题答案等);细察,细查;(用计算机)搜索,检索
n. 搜寻,搜查;探索,寻求;(计算机的)搜索,检索;<律>调查地产负担
[^12]: replacement /rɪˈpleɪsmənt/
n. 代替,更换;取代(或代替)的人(或物);补充兵员
[^13]: replace /rɪˈpleɪs/
v. 取代;(用……)替换,(以……)接替;更换, 赔还;把…放回原处
[^14]: substituted /ˈsʌbstɪtju:tɪd/
adj. 取代的,代替的
v. 代替;用……代替(substitute 的过去式)
[^15]: terminate /ˈtɜːmɪneɪt/
v.(使)结束,(使)终止;到达终点站;终止妊娠,人工流产;<美>解雇;<美>谋杀(某人);在……结尾,以……收尾
adj. 结束的
[^16]: greater /greɪtə®/
adj. 较大的(great 的比较级)