当前位置:网站首页>【C和指针第14章】预处理器
【C和指针第14章】预处理器
2022-07-24 11:39:00 【小原小原吃汤圆】
第14章 预处理器
14.1 预定义符号
#include<stdio.h>
int main()
{
printf("进行编译的源文件名:%s \n",__FILE__);
printf("文件当前行的行号:%d \n",__LINE__);
printf("文件被编译的日期:%s\n",__DATE__);
printf("文件被编译的时间:%s\n",__TIME__);
printf("编译器是否为ANSI C:%d\n",__STDC__);
return 0;
}
==========================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
进行编译的源文件名:test.c
文件当前行的行号:7
文件被编译的日期:Jul 21 2022
文件被编译的时间:15:04:21
编译器是否为ANSI C:1
14.2 #define
#include<stdio.h>
#define DEBUG_PRINT printf("File %s line %d:"\ "x=%d,y=%d,z=%d",\ __FILE__,__LINE__,\ x,y,z)
int main()
{
int x=1,y=1,z=1;
x *= 2;
y += x;
z = x*y;
DEBUG_PRINT;
return 0;
}
=====================================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
File test.c line 13:x=2,y=3,z=6
#include<stdio.h>
#define PROCESS_LOOP for(int i=0;i<10;i++) \ {
\ printf("value=%d\n",i); \ }
int main()
{
PROCESS_LOOP;
return 0;
}
===================================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
value=0
value=1
value=2
value=3
value=4
value=5
value=6
value=7
value=8
value=9
14.2.1 宏
#include<stdio.h>
#define SQUARE(x) x*x
int main()
{
printf("result = %d",SQUARE(6));
return 0;
}
=========================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
result = 36
#include<stdio.h>
#define SQUARE(x) x*x
int main()
{
int a=5;
printf("result = %d",SQUARE(a+1));
return 0;
}
=========================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
result = 11
#include<stdio.h>
#define SQUARE(x) (x)*(x)
int main()
{
int a=5;
printf("result = %d",SQUARE(a+1));
return 0;
}
=======================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
result = 36
#include<stdio.h>
#define DOUBLE(x) (x)+(x)
int main()
{
int a=5;
printf("result = %d",10*DOUBLE(a));
return 0;
}
=====================================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
result = 55
#include<stdio.h>
#define DOUBLE(x) ((x)+(x))
int main()
{
int a=5;
printf("result = %d",10*DOUBLE(a));
return 0;
}
===========================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
result = 100
14.2.2 #define替换
#include<stdio.h>
#define PRINT(FORMAT,VALUE) \ printf("The value is " FORMAT "\n",VALUE)
int main()
{
PRINT("%d",24);
return 0;
}
===============================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
The value is 24
#include<stdio.h>
#define PRINT(FORMAT,VALUE) \ printf("The value of " #VALUE \ " is " FORMAT "\n",VALUE)
int main()
{
int x = 20;
PRINT("%d",x+3);
return 0;
}
========================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
The value of x+3 is 23
#include<stdio.h>
#define ADD_TO_SUM(sum_number,value) \ sum ## sum_number += value
int main()
{
int sum1 = 20;
ADD_TO_SUM(1,10);
printf("sum1的值为:%d",sum1);
return 0;
}
============================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
sum1的值为:30
14.2.3 宏与函数
#include<stdio.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
int main()
{
int a = 10,b = 15;
printf("MAX = %d",MAX(a,b));
return 0;
}
===================================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
MAX = 15
#include<stdio.h>
#include<stdlib.h>
#define MALLOC(n,type) \ ((type *)malloc((n) * sizeof(type)))
int main()
{
int *p = MALLOC(25,int);
if(p!=NULL)
printf("OK");
return 0;
}
===========================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
OK
14.2.4 带副作用的宏参数
#include<stdio.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
int main()
{
int x=5,y=8;
int z = MAX(x++,y++);
printf("x = %d,y = %d,z = %d",x,y,z);
return 0;
}
===============================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
x = 6,y = 10,z = 9
14.2.5 命名约定
14.2.6 #undef

14.2.7 命令行定义
14.3 条件编译
#include<stdio.h>
#define DEBUG 1
int main()
{
#if DEBUG
printf("DEBUG is 1 \n");
#endif
return 0;
}
===========================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
DEBUG is 1
#include<stdio.h>
#define DEBUG1 0
#define DEBUG2 0
#define DEBUG3 1
int main()
{
#if DEBUG1
printf("DEBUG1 \n");
#elif DEBUG2
printf("DEBUG2 \n");
#elif DEBUG3
printf("DEBUG3 \n");
#endif
return 0;
}
=================================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
DEBUG3
14.3.1 是否被定义
#include<stdio.h>
// #define symbol 0
#define X 0
#define ABC 0
#define BCD 0
int main()
{
// #if defined(symbol)
// printf("symbol has been defined");
// #endif
// #ifdef symbol
// printf("symbol has been defined");
// #endif
// #if !defined(symbol)
// printf("symbol has not been defined");
// #endif
// #ifndef symbol
// printf("symbol has been defined");
// #endif
#if X>0||(defined(ABC) &&defined(BCD))
printf("X>0||(defined(ABC) &&defined(BCD))");
#endif
return 0;
}
14.3.2 嵌套指令
#include<stdio.h>
#define OS_UNIX 0
#define OPTION2 0
int main()
{
#if defined(OS_UNIX)
#ifdef OPTION1
printf("OPTION1");
#endif
#ifdef OPTION2
printf("OPTION2");
#endif
#elif defined(OS_MSDOS)
#ifdef OPTION2
printf("OPTION2");
#endif
#endif
return 0;
}
=============================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc tempCodeRunnerFile.c -o tempCodeRunnerFile } ; if ($?) {
.\tempCodeRunnerFile }
OPTION2
14.4 文件包含
14.4.1 函数库文件包含
14.4.2 本地文件包含
14.4.3 嵌套文件包含
//test.h
#ifndef _test_H
#define _test_H 1
#endif
========================================================
//test.c
#include<stdio.h>
#include "test.h"
int main()
{
printf("%d",_test_H);
return 0;
}
============================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
1
14.5 其他指令
#include<stdio.h>
// #define OPTION_A 1
int main()
{
#if defined(OPTION_A)
printf("123");
#else
#error no defined
#endif
return 0;
}
==============================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
test.c: In function 'main':
test.c:9:10: error: #error no defined
#error no defined
#include<stdio.h>
#line 10 "string"
//第10行
int main() //第11行
{
//第12行
printf("%d %s",__LINE__,__FILE__); //第13行
return 0;
}
===========================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
13 string
14.6 总结
14.7 警告的总结
14.8 编程提示的总结
边栏推荐
- MySQL advanced (XVII) cannot connect to database server problem analysis
- Easy to understand ES6 (IV): template string
- L1-059 敲笨钟
- High frequency written test questions (Weilai)
- Sentinel vs Hystrix 限流对比,到底怎么选?
- 安装jmeter
- Common formulas and application scenarios of discrete distribution
- 离散分布常用公式及应用场景
- Hash - 15. Sum of three numbers
- Hash - 1. Sum of two numbers - some people fall in love during the day, some people watch the sea at night, and some people can't do the first question
猜你喜欢

Is there any charge for PDF processing? impossible!
![MOS tube - Notes on rapid recovery application (I) [principle]](/img/a1/8427c9b1d0ea0cecce820816510045.png)
MOS tube - Notes on rapid recovery application (I) [principle]
![[deserialization vulnerability-01] Introduction to serialization and deserialization](/img/e4/6b9ee6ee74f3cdc3c886ed3af9ef73.png)
[deserialization vulnerability-01] Introduction to serialization and deserialization

How to go from functional testing to automated testing?

一周精彩内容分享(第13期)

Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing

One week's wonderful content sharing (issue 13)

JMeter if controller

PDF处理还收费?不可能!

Recommended SSH cross platform terminal tool tabby
随机推荐
【反序列化漏洞-01】序列化与反序列化简介
HCIP OSPF接口网络类型实验 第四天
One week's wonderful content sharing (issue 13)
C#入门系列(二十九) -- 预处理命令
Shell script "< < EOF" my purpose and problems
CSDN会员的魅力何在?我要他有什么用?
Record a garbage collection and analysis of gceasy
视频回放 | 如何成为一名优秀的地学和生态学领域的国际期刊审稿人?
Easy to understand ES6 (IV): template string
DevOps及DevOps常用的工具介绍
字符串——剑指 Offer 05. 替换空格
Leetcode 257. all paths of binary tree
The difference between synchronized and lock locks
The difference between YPbPr and YCbCr
How to choose sentinel vs. hystrix current limiting?
cgo+gSoap+onvif学习总结:9、go和c进行socket通信进行onvif协议处理
Imeta view | is short reading long amplicon sequencing applicable to the prediction of microbiome function?
哈希——15. 三数之和
What is cloud native? Why is cloud native technology so popular?
源码分析Sentry用户行为记录实现过程









































