当前位置:网站首页>(C语言)程序环境和预处理
(C语言)程序环境和预处理
2022-07-31 09:18:00 【银河罐头】
翻译环境
linux环境下的可执行程序也是elf格式
#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)"
//比如这种就不能替换
‘#’
使用 # ,把一个宏参数变成对应的字符串
#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>
查找头文件直接去标准路径下去查找,如果找不到就提示编译错误。
边栏推荐
猜你喜欢
随机推荐
loadrunner-Controller负载测试-各模块功能记录01测试场景设计
JSP session的生命周期简介说明
qt pass custom structure parameters in different threads
【Excel】生成随机数字/字符
第五章
MySQL 高级(进阶) SQL 语句 (一)
js implements the 2020 New Year's Day countdown bulletin board
Aleo Testnet3规划大纲
Binary tree search and backtracking problem (leetcode)
第二十四课、二十五课,高级光照(blinn),Gamma矫正
Scala basics [seq, set, map, tuple, WordCount, queue, parallel]
刷题《剑指Offer》day06
Kotlin—基本语法(一)
如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
Golang-based swagger super intimate and super detailed usage guide [there are many pits]
js right dot single page scrolling introduction page
ONES 入选 CFS 财经峰会「2022数字化创新引领奖」
如何将虚拟机上的文件复制到主机上
matlab常用符号用法总结
【NLP】Transformer理论解读