当前位置:网站首页>(C language) program environment and preprocessing
(C language) program environment and preprocessing
2022-07-31 09:33:00 【Galaxy Can】
翻译环境


linuxThe same goes for executable programs in the environmentelf格式
#define定义宏
#define SQUARE(X) X*X
int main()
{
int ret = SQUARE(5 + 1);
printf("%d\n", ret);
return 0;
}
11
5+1*5+1=11
#define SQUARE(X) ((X)*(X))
((5+1)*(5+1))
36
宏不能递归
当预处理器搜索#define定义的符号的时候,字符串常量的内容并不被搜索.
"SQUARE(3)"
//For example, this cannot be replaced
‘#’
使用 # ,把一个宏参数变成对应的字符串
#define PRINT(N) printf("the value of "#N" is %d\n",N)
//#N -> "a"
//N -> a
int main()
{
int a = 10;
PRINT(a);
int b = 20;
PRINT(b);
return 0;
}
the value of a is 10
the value of b is 20
#define PRINT(N,FORMAT) printf("the value of "#N" is "FORMAT"\n",N)
int main()
{
float f = 2.3f;
PRINT(f, "%lf");
return 0;
}
the value of f is 2.300000
‘##’
##可以把位于它两边的符号合成一个符号.
#define CAT(Str,Num) Str##Num
int main()
{
int Hello123 = 100;
printf("%d\n", CAT(Hello, 123));
//printf("%d\n", Hello123);
return 0;
}
100
带副作用的宏参数
#define MAX(x,y) (x>y?x:y)
int main()
{
int a = 5;
int b = 4;
int m = MAX(a++, b++);
//int m=((a++)>(b++)?(a++):(b++))
printf("m=%d\n", m);
printf("a=%d\n", a);
printf("b=%d\n", b);
return 0;
}
m=6
a=7
b=5
(5)>(4)? a->6,b->5
a++,后置++,先使用,m=6,后++,a->7
宏有时候可以做函数做不到的事情.比如:宏的参数可以出现类型,但是函数做不到
#define MALLOC(num,type) (type*)malloc((num)*sizeof(type));
int main()
{
//int* p=malloc(40);
int* p= MALLOC(10, int);
//int* p=(int*)malloc(10*sizeof(int));
return 0;
}
#undef
用于移除一个宏定义

条件编译

防止头文件被重复包含
方法1
#pragma once
int Add(int x, int y);
方法2
#ifndef __TEST_H__
#define __TEST_H__
int Add(int x,int y)
#endif
头文件被包含的方式
本地文件包含
#include “filename”
查找策略:先在源文件所在目录下查找,如果该头文件未找到,编译器就像查找库函数头文件一样在标
准位置查找头文件.
如果找不到就提示编译错误
库文件包含
#include <filename.h>
查找头文件直接去标准路径下去查找,如果找不到就提示编译错误.
边栏推荐
猜你喜欢

文件的逻辑结构与物理结构的对比与区别

服务器上解压文件时提示“gzip: stdin: not in gzip format,tar: Child returned status 1,tar: Error is not recovera“

基于学生成绩管理系统(附源代码及数据库)

来n遍剑指--09. 用两个栈实现队列

六、MFC文档类(单文档和多文档)

Come n times - 07. Rebuild the binary tree

感情危机,朋友的网恋女友要和他闹分手,问我怎么办

Pytorch学习记录(七):自定义模型 & Auto-Encoders

Chapter Six

Kotlin—基本语法 (四)
随机推荐
内联元素居中
UE4插件软链接(关联)
基于学生成绩管理系统(附源代码及数据库)
各位大佬,sqlserver 支持表名正则匹配吗
Pytorch学习记录(七):自定义模型 & Auto-Encoders
Come n times with the sword--05. Replace spaces
第五章
文件的逻辑结构与物理结构的对比与区别
spark过滤器
(C语言基础)原样输入输出
Kotlin—基本语法(三)
文件管理:目录管理
【RISC-V】risc-v架构学习笔记(架构初学)
loadrunner-Controller负载测试-各模块功能记录01测试场景设计
A Spark SQL online problem troubleshooting and positioning
Come n times - 07. Rebuild the binary tree
loadrunner脚本--添加集合点
使用turtle画按钮
零代码工具推荐 八爪鱼采集器
【节选】吴恩达给出的AI职业生涯规划