宏定义学习

【1】宏定义怎么理解?

       关于宏定义,把握住本质:仅仅是一种字符替换,而且是在预处理之前就进行。

【2】宏定义可以包括分号吗?

       可以,示例代码如下:

 1 #include<iostream>
 2 using  namespace std;
 3 
 4 #define PI  3.14; //宏定义可以包括“;”
 5 
 6 void main()
 7 {
 8     double r=10,s;
 9     s=r*r*PI              //注意此处的语法
10     cout<<s<<endl;        //314
11 }

【3】宏定义一种新类型如何实现?

       示例代码如下:

1 #include<iostream>
2 using  namespace std;
3 #define   int   int *
4 void main()
5 {
6     int a,b;//    int *a, b;
7 }
8     //理解此处的微妙:int *a,b;      这条语句同时定义了两个变量。一个指针:int *a;   一个变量:int  b;

【4】宏定义一个函数如何实现?

        示例代码如下:

 1 #include<iostream>
 2 using  namespace std;
 3 
 4 #define Begin()  {int a;a=0;cout<<"a="<<a<<endl;}  
 5 
 6 void main()
 7 {
 8     Begin()
 9 }
10 //如果{......}中的代码太多,应该使用宏连接
11 //代码如下所示:
12 #define  Begin()   { int i;\
13                     i=10;\
14                     cout<<"i="<<i<<endl;}

【5】宏定义如何取消?

        示例代码如下:

 1 #include<iostream>
 2 using  namespace std;
 3 
 4 #define int int *  
 5 
 6 void main()
 7 {
 8     int a, p;  // int *a,p;
 9     a = &p;
10     #undef int     //取消宏定义
11     int b = 10;
12     a = &b;
13 }

 【6】对宏定义歧义现象怎么识别?

         示例代码如下:

1 #define  SUM(x,y)     x*y
2 #define  SUMM(x,y)    ((x)*(y))
3 void  main()
4 {
5     int a = 4, b = 5;
6     cout<<SUM(a+2,b+4)<<endl;      //18
7     cout<<SUMM(a+2,b+4)<<endl;     //54
8 }

      求一个数的平方正确的宏定义:

#define   S(r)    ((r)*(r))

     这个宏定义注意事项:

    (1)宏名和参数的括号间不能有空格

    (2)宏替换只作替换,不做计算,不做表达式的求解

    (3)函数调用在编译后程序运行时进行,并且分配内存。宏替换在编译前执行,不分配内存

    (4)宏的哑实结合不存在类型,也没有类型转换

    (5)函数只有一个返回值,利用宏则可以设法得到多个值

    (6)宏展开使源程序变长,函数调用不会

    (7)宏展开不占运行时间,只占编译时间,函数调用占运行时间(分配内存 保留现场 值传递 返回值)

      何谓哑实结合?

      示例代码及解释如下:

1 #define    S(a,b)    a*b
2 void main()
3 {
4     int area = 0;
5     area = S(3,2);  //第一步:被替换为area = a*b; 第二步:被替换为area = 2*3;
6     //类似于函数调用,有一个哑实结合过程
7 }

 【7】下面宏定义特例如何解析?

         示例代码如下:

 1 #define   NAME    "zhangyuncong"
 2 //#define   AB     "liu       //error!!编译错误
 3 //#define     0x     abcd     //error!!编译错误
 4 void  main()
 5 {
 6     cout<<NAME<<endl;      //zhangyuncong
 7     cout<<"NAME"<<endl;    //NAME
 8     cout<<"NAMElist"<<endl;//NAMElist
 9     //cout<<NAMEList<<endl;  //error!!!!编译错误
10 }

       也就是说,这种情况下记住:#define    第一位置     第二位置

      (1)不替换程序中的字符串内的任何内容

      (2)第一位置只能是合法的标识符(可以是关键字)

      (3)第二位置如果有字符串,必须把“”配对

      (4)只替换与第一位置完全相同的标识符

        总之一句话:仅仅只是简单的替换而已,不要在中间计算结果,一定要替换出表达式之后再计算

 【8】宏定义的特例有参形式如何解析?

         示例代码如下:

1 #define   FUN(a)  "a" 
2 void  main()
3 {
4     cout<<FUN(345)<<endl;     //a
5     cout<<FUN(a)<<endl;       //a
6     cout<<FUN("a")<<endl;     //a
7     char *str=FUN(abc);   
8     cout<<str<<endl;          //a
9 }

       通过上例可以看到,如果这样写,不论实参是什么,都不会摆脱被替换为“a”的命运。也许,你会问,那么我要实现FUN(345)被替换为“345”??肿么办呢??

       请看下面这个用法

 【9】有参宏定义中#的有何作用?

         示例代码如下:

 1 #define STR(str) #str 
 2 
 3 void  main()
 4 {
 5     cout<<STR(abc)<<endl;      //abc
 6     cout<<STR("abc")<<endl;    //"abc"
 7     cout<<STR(123)<<endl;      //123
 8     cout<<STR(my#name)<<endl;  //my#name
 9 //    cout<<STR(()<<endl;        //error!!编译错误
10     cout<<STR(.)<<endl;        //.
11 //    cout<<STR(A,B)<<endl;      //error!!编译错误
12     cout<<STR(())<<endl;       //()
13     const char * str=STR(liuyong);
14     cout<<str<<endl;           //liuyong
15 }

        备注:代码编译环境为VS2010  那么相信“#”的作用也一目了然。在此不作赘述。

  【10】有参宏定义中##有何作用?

          示例代码如下:

 1 #define  SIGN( x ) INT_##x
 2 #define  WIDE(str)  L##str
 3 
 4 void main()
 5 {
 6     int  SIGN(a);
 7 //  int INT_a;       //error!!   redefinition
 8     char * WIDE(a);
 9 //  char *La;        //error!!   redefinition
10 }

  【11】当一个宏自己调用自己时,会发生什么呢?

         例如:#define  TEST(x)   ( x + TEST( x ) )

         TEST(1); 会发生什么呢?为了防止无限制递归展开,语法规定:当一个宏遇到自己时,就停止展开。

         也就是说,当对TEST(1)进行展开时,展开过程中又发现了一个TEST,那么就将这个TEST当作一个

         一般的符号。TEST(1)最终被展开为:1 + TEST(1)。

  【12】可以举一个变参宏的例子吗?

         示例代码如下

1 #define LOG( format,... )  printf( format, __VA_ARGS__ )
2 
3 void main()
4 {
5     int a = 10;
6     char *str = "abc";
7     LOG("%d,%s",a,str); //10,abc
8 }

  【13】当宏作为参数被放进另一个宏体时,将会发生什么?

            当一个宏参数被放进宏体时,这个宏参数会首先被全部展开(当然,没有绝对,也有例外)。当展开后的宏参数被放进宏体时,

         预处理器对新展开的宏体进行第二次扫描。并继续展开。举例说明:

         示例代码如下:

1 #define PARAM(x)  x
2 #define ADDPARAM(x)  INT_##x
3 
4 void main()
5 {
6     int PARAM(ADDPARAM(1));
7 //    int INT_1;   //error!!  编译错误  重复定义
8  }

         因为ADDPARAM(1)是作为PARAM的宏参数,所以先将ADDPARAM(1)展开为INT_1,然后再将INT_1放进PARAM。

         也有例外,如果PARAM宏内对宏参数使用了# 或者 ## ,那么宏参数不再被展开。例如:

         #define PARAM( x ) #x
         #define ADDPARAM( x ) INT_##x
         PARAM( ADDPARAM( 1 ) ); 将被展开为"ADDPARAM( 1 )"。

 

Good Good Study, Day Day Up.

顺序 选择 循环 总结

转载于:https://www.cnblogs.com/Braveliu/archive/2012/12/28/2837954.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274406.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

学习 koa 源码的整体架构,浅析koa洋葱模型原理和co原理

前言这是学习源码整体架构系列第七篇。整体架构这词语好像有点大&#xff0c;姑且就算是源码整体结构吧&#xff0c;主要就是学习是代码整体结构&#xff0c;不深究其他不是主线的具体函数的实现。本篇文章学习的是实际仓库的代码。学习源码整体架构系列文章如下&#xff1a;1.…

公网对讲机修改对讲机程序_更少的对讲机,对讲机-更多专心,专心

公网对讲机修改对讲机程序重点 (Top highlight)I often like to put a stick into the bike wheel of the UX industry as it’s strolling along feeling proud of itself. I believe — strongly — that as designers we should primarily be doers not talkers.我经常喜欢在…

spring配置文件-------通配符

<!-- 这里一定要注意是使用spring的mappingLocations属性进行通配的 --> <property name"mappingLocations"> <list> <value>classpath:/com/model/domain/*.hbm.xml</value> </list> </proper…

若川知乎问答:2年前端经验,做的项目没什么技术含量,怎么办?

知乎问答&#xff1a;做了两年前端开发&#xff0c;平时就是拿 Vue 写写页面和组件&#xff0c;简历的项目经历应该怎么写得好看&#xff1f;以下是我的回答&#xff0c;阅读量5000&#xff0c;所以发布到公众号申明原创。题主说的2年经验做的东西没什么技术含量&#xff0c;应…

ui设计基础_我不知道的UI设计的9个重要基础

ui设计基础重点 (Top highlight)After listening to Craig Federighi’s talk on how to be a better software engineer I was sold on the idea that it is super important for a software engineer to learn the basic principles of software design.听了克雷格费德里希(C…

Ubuntu下修改file descriptor

要修改Ubuntu下的file descriptor的话&#xff0c;请参照一下步骤。&#xff08;1&#xff09;修改limits.conf  $sudo vi /etc/security/limits.conf  增加一行  *  -  nofile  10000&#xff08;2&#xff09;修改 common-session  $ sudo vi/etc/pam.d/common…

C# 多线程控制 通讯 和切换

一.多线程的概念   Windows是一个多任务的系统&#xff0c;如果你使用的是windows 2000及其以上版本&#xff0c;你可以通过任务管理器查看当前系统运行的程序和进程。什么是进程呢&#xff1f;当一个程序开始运行时&#xff0c;它就是一个进程&#xff0c;进程所指包括运行中…

vue路由匹配实现包容性_包容性设计:面向老年用户的数字平等

vue路由匹配实现包容性In Covid world, a lot of older users are getting online for the first time or using technology more than they previously had. For some, help may be needed.在Covid世界中&#xff0c;许多年长用户首次上网或使用的技术比以前更多。 对于某些人…

IPhone开发 用子类搞定不同的设备(iphone和ipad)

用子类搞定不同的设备 因为要判断我们的程序正运行在哪个设备上&#xff0c;所以&#xff0c;我们的代码有些混乱了&#xff0c;IF来ELSE去的&#xff0c;记住&#xff0c;将来你花在维护代码上的时间要比花在写代码上的时间多&#xff0c;如果你的项目比较大&#xff0c;且IF语…

见证开户_见证中的发现

见证开户Each time we pick up a new video game, we’re faced with the same dilemma: “How do I play this game?” Most games now feature tutorials, which can range from the innocuous — gently introducing each mechanic at a time through natural gameplay — …

使用JXL组件操作Excel和导出文件

使用JXL组件操作Excel和导出文件 原文链接&#xff1a;http://tianweili.github.io/blog/2015/01/29/use-jxl-produce-excel/ 前言&#xff1a;这段时间参与的项目要求做几张Excel报表&#xff0c;由于项目框架使用了jxl组件&#xff0c;所以把jxl组件的详细用法归纳总结一下。…

facebook有哪些信息_关于Facebook表情表情符号的所有信息

facebook有哪些信息Ever since worldwide lockdown and restriction on travel have been imposed, platforms like #Facebook, #Instagram, #Zoom, #GoogleDuo, & #Whatsapp have become more important than ever to connect with your loved ones (apart from the sourc…

M2总结报告

团队成员 李嘉良 http://home.cnblogs.com/u/daisuke/ 王熹 http://home.cnblogs.com/u/vvnx/ 王冬 http://home.cnblogs.com/u/darewin/ 王泓洋 http://home.cnblogs.com/u/fiverice/ 刘明 http://home.cnblogs.com/u/liumingbuaa/ 由之望 http://www.cnbl…

react动画库_React 2020动画库

react动画库Animations are important in instances like page transitions, scroll events, entering and exiting components, and events that the user should be alerted to.动画在诸如页面过渡&#xff0c;滚动事件&#xff0c;进入和退出组件以及应提醒用户的事件之类的…

Weather

public class WeatherModel { #region 定义成员变量 private string _temperature ""; private string _weather ""; private string _wind ""; private string _city ""; private …

线框模型_进行计划之前:线框和模型

线框模型Before we start developing something, we need a plan about what we’re doing and what is the expected result from the project. Same as developing a website, we need to create a mockup before we start developing (coding) because it will cost so much…

撰写论文时word使用技巧(转)

------------------------------------- 1. Word2007 的表格自定义格式额度功能是很实用的&#xff0c;比如论文中需要经常插入表格的话&#xff0c; 可以在“表格设计”那里“修改表格样式”一次性把默认的表格样式设置为三线表&#xff0c;这样&#xff0c; 你以后每次插入的…

工作经验教训_在设计工作五年后获得的经验教训

工作经验教训This June it has been five years since I graduated from college. Since then I’ve been working as a UX designer for a lot of different companies, including a start-up, an application developer, and two consultancy firms.我从大学毕业已经五年了&a…

Wayland 源码解析之代码结构

来源&#xff1a;http://blog.csdn.net/basilc/article/details/8074895 获取、编译 Wayland 及其依赖库可参考 Wayland 官方网站的 Build 指南&#xff1a;http://wayland.freedesktop.org/building.html。 Wayland 实现的代码组成可以分成以下四部分&#xff1a; 1. Wayland…

中文排版规则_非设计师的5条排版规则

中文排版规则01仅以一种字体开始 (01 Start with only one font) The first tip for non-designers dealing with typography is simple and will make your life much easier: Stop combining different fonts you like individually and try using only one font in your fut…