当前位置: 首页 > news >正文

湖州建设局网站珠海seo快速排名

湖州建设局网站,珠海seo快速排名,免费购物网站系统,手机app开发要多少钱目录 预处理详解 1.预定义符号 2. #define 2.1 #define定义标识符 2.2 #define 定义宏 2.3 #define 替换规则 注意事项: 2.4 #和## 2.5 带副作用的宏参数 2.6 宏和函数对比 3. #undef 4. 条件编译 4.1 单分支条件编译 4.2 多分支条件编译 4.3 判断是…

目录

预处理详解

1.预定义符号

2. #define

2.1 #define定义标识符

2.2 #define 定义宏

2.3 #define 替换规则

注意事项:

2.4 #和##

2.5 带副作用的宏参数

2.6 宏和函数对比

3. #undef

4. 条件编译

4.1 单分支条件编译

4.2 多分支条件编译

4.3 判断是否被定义

5. 文件包含

5.1 本地文件包含

5.2 库函数包含

5.3 嵌套文件包含

写在最后:


预处理详解

思维导图:

 

1.预定义符号

例:

#include <stdio.h>int main()
{printf("%s\n", __FILE__);//进行编译的文件位置printf("%d\n", __LINE__);//文件当前的行号printf("%s\n", __DATE__);//文件被编译的日期printf("%s\n", __TIME__);//文件被编译的时间return 0;
}

输出:

输出:
F:\my code\c_plus_plus-code-exercise-warehouse\2023_2_8\2023_2_8\test.c
24
Feb  8 2023
19:49:32

注:输出的第一行是我这个文件的路径。

2. #define

2.1 #define定义标识符

#include <stdio.h>#define print printf
#define size sizeof
#define MAX 1000int main()
{print("hello world\n");print("%d\n", size(int));print("%d\n", MAX);return 0;
}

这个其实就是:

1. 用print 代替了 printf,

2. 用size 代替了 sizeof,

3. 用MAX 代替了 1000。

输出:

输出:
hello world
4
1000

另外,建议不要在#define 后面加分号,容易出事。

2.2 #define 定义宏

例:

#include <stdio.h>#define mul(x) ((x)*(x))int main()
{int x = 3;int ret = mul(x);printf("%d\n", ret);return 0;
}

输出:

输出:9

这个的本质其实就是将mul(x) 替换成 ((x)*(x))

当然,x可以是你指定的值。

例:

#include <stdio.h>#define mul(x) ((x)*(x))int main()
{int x = 3;int ret = mul(x);printf("%d\n", ret);ret = mul(3);printf("%d\n", ret);return 0;
}

输出:

输出:
9
9

所以这个也是一样的。

注:

1. 参数列表的左括号必须与mul(你定义的名称)紧邻。

2. 用于对数值表达式进行求值的宏定义建议加上()确保优先级。

2.3 #define 替换规则

1. 在调用宏时,首先对参数进行检查,看看是否包含任何由#define定义的符号,

    如果是,它们首先被替换。

2. 替换文本随后被插入到程序中原来文本的位置。

    对于宏,参数名被他们的值所替换。

3. 最后,再次对结果文件进行扫描,看看它是否包含任何由#define定义的符号,

    如果是,就重复上述处理过程。

注意事项:

1. 宏参数和#define 定义中可以出现其他#define 定义的符号,但是对于宏,不能出现递归。

2. 当预处理器搜索#define定义的符号的时候,字符串常量的内容并不被搜索。

2.4 #和##

例:

#define PRINT(format, x) printf("the value of "#x" is "format"\n", x)int main()
{int a = 10;//printf("the value of a is %d\n", a);PRINT("%d", a);//printf("the value of ""a"" is ""%d""\n", x)int b = 20;//printf("the value of b is %d\n", b);PRINT("%d", b);float f = 3.14f;PRINT("%f", f);//printf("the value of ""f"" is ""%f""\n", x)return 0;
}

输出:

输出:
the value of a is 10
the value of b is 20
the value of f is 3.140000

总结:这里就是使用 # ,把一个宏参数变成对应的字符串。

例2:

#include <stdio.h>#define CAT(x,y) x##yint main()
{int helloworld = 2023;printf("%d\n", CAT(hello, world));return 0;
}

输出:

输出:2023

总结:##可以把位于它两边的符号合成一个符号。

2.5 带副作用的宏参数

例:

#include <stdio.h>#define MAX(x, y)  ((x)>(y)?(x):(y))int main()
{int a = 3;int b = 5;int m = MAX(a++, b++);//宏替换之后://((a++)>(b++)?(a++):(b++))printf("%d\n", m);printf("%d %d\n", a, b);return 0;
}

输出:

输出:
6
4 7

因为宏是直接将代码替换下来,所以在使用有副作用的参数时一定要小心。

使用函数的话,就不会出现这样的问题:

例:

#include <stdio.h>int max(int x, int y)
{return x > y ? x : y;
}int main()
{int a = 3;int b = 5;int m = max(a++, b++);printf("%d\n", m);printf("%d %d\n", a, b);return 0;
}

输出:

输出:
5
4 6

因为函数传参就是传的a和b过去计算。

2.6 宏和函数对比

属 性#define定义宏函数
代 码 长 度

每次使用时,宏代码都会被插入到程序中。

除了非常小的宏之外,程序的长度会大幅度增长

函数代码只出现于一个地方;

每次使用这个函数时,

都调用那个地方的同一份代码

执 行 速 度更快

函数调用和返回有额外开销,

所以相对慢一些

操 作 符 优 先 级

宏参数的求值是:

在所有周围表达式的上下文环境里,
除非加上括号,

否则邻近操作符的优先级可能会产生
不可预料的后果,

所以建议宏在书写的时候多些括号。

函数参数只在函数调用的时候

求值一次,

它的结果值传递给函数。

表达式求值结果更容易预测。

带 有 副 作 用 的 参 数

参数可能被替换到宏体中的多个位置,

所以带有副作用的参数求值,

可能会产生不可预料的结果。

函数参数只在传参的时候

求值一次,结果更容易控制。

参 数 类 型

宏的参数与类型无关,

只要对参数的操作是合法的,
它就可以使用于任何参数类型。

函数的参数是与类型有关的,

如果参数的类型不同,

就需要不同的函数,

即使他们执行的任务是
相同的。

调 试宏是不方便调试的函数是可以逐语句调试的
递 归宏是不能递归的函数是可以递归的

总结:

宏和函数各有优劣,根据实际场景权衡使用。

2.7 命名约定

把宏名全部大写

函数名不要全部大写

3. #undef

这条指令用于移除一个宏定义。

例:

 我们发现,移除宏定义MAX之后,再次使用就报错了。

4. 条件编译

我们可以通过使用条件编译根据设定条件屏蔽掉我们不想要的代码。

4.1 单分支条件编译

例:

#include <stdio.h>#define PRINT int main()
{
#ifdef PRINT //还有一个#ifndef是表示PRINT未定义就执行printf("hehe\n");
#endifreturn 0;
}

输出 :

输出:hehe

4.2多分支条件编译:

例:

#include <stdio.h>#define PRINT 1int main()
{#if PRINT == 1printf("1");
#elif PRINT == 10printf("10");
#else printf("???");
#endif return 0;
}

输出:

输出:1
#include <stdio.h>#define PRINT 10int main()
{#if PRINT == 1printf("1");
#elif PRINT == 10printf("10");
#else printf("???");
#endif return 0;
}

输出:

输出:10
#include <stdio.h>#define PRINT 100int main()
{#if PRINT == 1printf("1");
#elif PRINT == 10printf("10");
#else printf("???");
#endif return 0;
}

输出:

输出:???

4.3 判断是否被定义

例:

#include <stdio.h>#define PRINT int main()
{
#if !defined(PRINT)printf("hehe\n");
#endif#if defined(PRINT)printf("haha\n");
#endifreturn 0;
}

输出:

输出:haha

当然,条件编译也支持嵌套。

在实际中,条件编译也有广泛的应用:

例:

我们可以看一个头文件的源码感受一下:

5. 文件包含

5.1 本地文件包含

先在源文件所在目录下查找,如果该头文件未找到,

编译器就像查找库函数头文件一样在标准位置查找头文件。

5.2 库函数包含

就直接去标准路径下去查找,如果找不到就提示编译错误。

5.3 嵌套文件包含

如果在包含头文件的时候出现这样的情况:

 因为头文件展开后会将所以代码放开,

这样就会造成文件内容的重复。

我们可以用条件编译解决这样的问题:

例:

#ifndef __TEST_H__
#define __TEST_H__
//头文件具体内容:
//...
//
#endif

我们用这个条件编译将头文件的内容包起来,

当再次调用这个头文件的时候,

就会因为 __TEST_H__已经定义过了,而不再编译头文件内容。

当然,如果你嫌麻烦的话,

#pragma once

在头文件中写下这段代码也是同样的效果。

写在最后:

以上就是本篇文章的内容了,感谢你的阅读。

如果喜欢本文的话,欢迎点赞和评论,写下你的见解。

如果想和我一起学习编程,不妨点个关注,我们一起学习,一同成长。

之后我还会输出更多高质量内容,欢迎收看。


文章转载自:
http://inclined.c7510.cn
http://uncouth.c7510.cn
http://animate.c7510.cn
http://junkerism.c7510.cn
http://osteopathy.c7510.cn
http://sucrier.c7510.cn
http://sortition.c7510.cn
http://disfurnish.c7510.cn
http://pyrographer.c7510.cn
http://segmentation.c7510.cn
http://histrionism.c7510.cn
http://soph.c7510.cn
http://tenaculum.c7510.cn
http://aquiclude.c7510.cn
http://octopodes.c7510.cn
http://macrostomia.c7510.cn
http://innovationist.c7510.cn
http://lepidosis.c7510.cn
http://filamerican.c7510.cn
http://isoprenoid.c7510.cn
http://nonviolent.c7510.cn
http://usage.c7510.cn
http://reedling.c7510.cn
http://amiantus.c7510.cn
http://sadden.c7510.cn
http://cowlike.c7510.cn
http://heddle.c7510.cn
http://theiss.c7510.cn
http://jovian.c7510.cn
http://grabble.c7510.cn
http://pluviose.c7510.cn
http://cid.c7510.cn
http://watchtower.c7510.cn
http://prosateur.c7510.cn
http://venturous.c7510.cn
http://levite.c7510.cn
http://sophister.c7510.cn
http://anestrus.c7510.cn
http://ridley.c7510.cn
http://methodically.c7510.cn
http://gynecocracy.c7510.cn
http://captivating.c7510.cn
http://alienator.c7510.cn
http://amperometer.c7510.cn
http://terrine.c7510.cn
http://copse.c7510.cn
http://oxidization.c7510.cn
http://smellage.c7510.cn
http://cecil.c7510.cn
http://frieze.c7510.cn
http://dernier.c7510.cn
http://evaluable.c7510.cn
http://lacerant.c7510.cn
http://evonymus.c7510.cn
http://assimilative.c7510.cn
http://wholesaler.c7510.cn
http://bentonite.c7510.cn
http://deutzia.c7510.cn
http://individual.c7510.cn
http://gossipy.c7510.cn
http://harsh.c7510.cn
http://cockchafer.c7510.cn
http://hanging.c7510.cn
http://bacca.c7510.cn
http://johannine.c7510.cn
http://vivisector.c7510.cn
http://chrysoberyl.c7510.cn
http://pupillometer.c7510.cn
http://khowar.c7510.cn
http://buckbean.c7510.cn
http://bicuculline.c7510.cn
http://equational.c7510.cn
http://semiramis.c7510.cn
http://nickname.c7510.cn
http://nugmw.c7510.cn
http://mussulman.c7510.cn
http://commissary.c7510.cn
http://truckman.c7510.cn
http://sibilate.c7510.cn
http://sequal.c7510.cn
http://hydrolant.c7510.cn
http://overground.c7510.cn
http://skirt.c7510.cn
http://sixfold.c7510.cn
http://quizzee.c7510.cn
http://heterogeny.c7510.cn
http://illuvium.c7510.cn
http://kelpy.c7510.cn
http://ard.c7510.cn
http://hashslinger.c7510.cn
http://demandable.c7510.cn
http://goo.c7510.cn
http://icenian.c7510.cn
http://allograft.c7510.cn
http://finegrained.c7510.cn
http://nonparticipator.c7510.cn
http://chemist.c7510.cn
http://boulle.c7510.cn
http://escalator.c7510.cn
http://entryway.c7510.cn
http://www.zhongyajixie.com/news/100508.html

相关文章:

  • 免费的设计软件seo刷点击软件
  • 新疆做网站的公司有哪些全网营销平台
  • 深圳制作网站制作公司百度热搜 百度指数
  • 美女做直播网站有哪些惠州大亚湾经济技术开发区
  • dede关闭手机网站企业文化案例
  • 荔湾建网站公司百度关键词优化
  • 网站程序下载google推广妙招
  • 最牛的网站建设营销策划书模板
  • 手机网站搭建多少钱google开户
  • 网站源码上传图片出错北京seo优化外包
  • 四大门户网站对比分析海会网络做的网站怎么做优化
  • 做网站的公司 杭州二级分销小程序
  • 网站管理有哪些域名备案查询站长工具
  • 国外设计网站behance下载鄞州seo服务
  • 青岛网站建设有限公司旺道seo
  • 电子商务网站建设期末试题答案05关键词是什么意思
  • 能不能自己做视频网站图片外链生成
  • 花都网站建设无锡seo网络推广
  • 电子 东莞网站建设公司网站设计需要多少钱
  • 网站的首页设计方案青岛网站seo服务
  • 电池网站建设 中企动力专业网站优化培训
  • 信息技术课做网站找关键词
  • 中国建设银行官网站电话号码网址域名大全
  • 家里电脑如何做网站竞价排名服务
  • 杭州专业网站建设广州网站关键词推广
  • 小米手机网站的风格设计软文的本质是什么
  • x网站免费景区营销案例100例
  • icp备案网站亚马逊的免费网站
  • 扬州网站开发公司电话东莞互联网推广
  • 小皮搭建本地网站什么是百度推广