define使用方法及模板

2021-08-15 18:40:00 字數 1235 閱讀 3080

#define       pi            (3.1415926)

#define      max(a,b)          ((a) > (b) ? (a) : (b))

#define  a(x)            ##x

#define  b(x)            #@x

#define  c(x)            #x

如果我們假設x=a,那麼a(a)就是a,b(a)就是『a』,c(1)就是」a「

// 字串轉化用法

#define   to_string( s )     #s 

編譯前:

cout < < to_string( hello world! ) < < endl; 

編譯後:

cout < < "hello world!" < < endl; 

//  變數名的預編譯聯結用法

#define   to_value(  a,  b  )     a#

#b編譯前:

cout < < to_value(  plea, se ) < < endl; 

編譯後:

cout < <  

please

< < endl; 

#define     macro( arg1,   arg2  )       do while(0)

定義巨集:#define

取消巨集:#undef

#ifdef … (#else) … #endif

#if 1  …  (#else) … #endif

#ifndef   _hello_h_

#define   _hello_h_

...//檔案內容

...#endif

// mfc訊息路由巨集

#define begin_message_map(theclass, baseclass) \

ptm_warning_disable \

const afx_msgmap* theclass::getmessagemap() const \

\const afx_msgmap* pascal theclass::getthismessagemap() \

\}; \

static const afx_msgmap messagemap = \

; \return &messagemap; \

}

define的一些使用方法

1.可以對一些常量 字串進行巨集定義,在預處理過程中進行替換,而不是在編譯過程中。define pi 3.1415926 2.也可以對資料型別進行巨集定義,這樣與 typedef 效果相同 typedef為 c語言的關鍵字,作用是為一種資料型別定義乙個新的名字。typedef unsigned ch...

C和C 中 define的使用方法

define常規的文字替換就不多說明了,先說一下帶引數的巨集替換,比如 define add x,y x y 需要注意的就是在涉及運算或著其他一些情況下,要加上括號來避免結合律影響運算結果,像5 add 2,3 你期望的結果是25,但是,在不加括號的情況下 5 2 3 結果是30.當需要換行時,需要...

C Cache何時使用及使用方法

cache 即快取記憶體.那麼cache是怎麼樣提高系統效能與執行速度呢?是不是在任何情況下用cache都能提高效能?是不是cache用的越多就越好呢?我在近 期開發的專案中有所體會,寫下來當作總結也希望能跟大家一起 有錯誤的地方希望大家批評指正。1.cache 是怎麼樣工作的?cache 是分配在...