当前位置:网站首页>【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 编程提示的总结
边栏推荐
- Record a garbage collection and analysis of gceasy
- L1-049 天梯赛座位分配
- C # entry series (29) -- preprocessing commands
- oracle 11.2.0.4 asm单实例不随系统启动而自动开启
- Judge whether a group of cards can become shunzi (the size of the king is 14,15)
- String - 541. Reverse string II
- CCF 201803_ 1 jump jump
- JVM visualvm: multi hop fault handling tool
- Text message verification of web crawler
- C#入门系列(二十九) -- 预处理命令
猜你喜欢

JPS has no namenode and datanode reasons

Record a garbage collection and analysis of gceasy
![08 [AIO programming]](/img/a6/156cb97e653190c76f22c88b758fef.png)
08 [AIO programming]

生信周刊第37期
![[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)](/img/a5/c92e0404c6a970a62595bc7a3b68cd.gif)
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)

iMeta观点 | 短读长扩增子测序是否适用于微生物组功能的预测?

视频回放 | 如何成为一名优秀的地学和生态学领域的国际期刊审稿人?

Shell script "< < EOF" my purpose and problems

Text message verification of web crawler

Stream stream
随机推荐
Grep actually uses ps/netstat/sort
How to go from functional testing to automated testing?
[golang] golang implements the string interception function substr
Operational amplifier - Notes on rapid recovery [1] (parameters)
Svn server and client installation (Chinese package) and simple use
使用Prometheus+Grafana实时监控服务器性能
MySQL creates partition tables and automatically partitions them by day
运算放大器 —— 快速复苏笔记[壹](参数篇)
【C和指针第11章】动态内存分配
Common formulas and application scenarios of discrete distribution
Jmeter-If控制器
Leetcode 257. 二叉树的所有路径
2 万字详解,吃透 ES!
ctfshow ThinkPHP专题 1
JMeter runtime controller
[QNX Hypervisor 2.2用户手册]9.2 cmdline
Use prometheus+grafana to monitor server performance in real time
How to choose sentinel vs. hystrix current limiting?
CSDN会员的魅力何在?我要他有什么用?
MySql的DDL和DML和DQL的基本语法









































