C:宏定義

來源:互聯網
上載者:User

使用宏時,用圓括弧括住每個參數,並括住宏的整體定義;一般來說,在宏中不要使用自增或自減運算子。

[1]最值

#define MIN(x, y)  (((x) < (y)) ? (x) : (y))

求兩者間的較小者

#define MAX(x, y)  (((x) > (y)) ? (x) : (y))

求兩者間的較大者

#define MAX3(x,y,z) MAX(MAX(x,y),z)

求三者間的較大者

[2]移位

#define SHIFT(value, n) (((n) > 0) ? ((value) << (n)) 

: ((value) >> -(n)))

對value進行移位操作,n > 0左移位,n < 0右移位

[3]字元

#define IS_LOWER_CASE(x) (((x)>='a') && ((x)<='z'))

判斷小寫

#define IS_UPPER_CASE(x) (((x)>='A') && ((x)<='Z'))

判斷大寫

#define IS_ALPHABETIC(c) (IS_LOWER_CASE (c) || IS_UPPER_CASE (c))

判斷字母

#define IS_DIGIT(x) (((x)>='0') && ((x)<='9'))

判斷數字

#define IS_SPECIAL(c)  (!IS_DIGIT (c) && !IS_ALPHABETIC (c))

判斷特殊字元

[4]取絕對值

#define ABSOLUTE_VALUE(x) (((x) < 0) ? -(x) : (x))

[5]#運算子

#define dprint(expr) printf(#expr " = %.1fn",expr)

#參數:將輸出帶引號的參數值;

dprint(1.0/2.0);將輸出:1.0/2.0=0.5;

[6]##運算子

#define xprint(front,back) front ## back

xprint(x,1);將輸出:x1

[7]判斷閏年

#define LEAP_YEAR(x) (((x) % 4 == 0 && (x) % 100 != 0) || ((x) % 400 == 0))

[8]整數val取第n位的bit

#define BIT(VAL,N) (((VAL) >> (N)) & 1 ? 1 : 0)

本文出自 “子 孑” 部落格,請務必保留此出處http://zhangjunhd.blog.51cto.com/113473/54402

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.