宏定義學習

【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.pswp.cn/news/274406.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274406.shtml
英文地址,請注明出處:http://en.pswp.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…