C語言的實用妙招,總有一款適合你
do{}while(0)
如果定義的宏函數(shù)后面有多條語句,使用這樣的方式會有問題:?#define FUNC() func1(); func2()?if(bRunF)
? ? ?FUNC();展開宏定義后會變成:?if(bRunF)
? ? ?func1();
? ? ?func2();邏輯就不對了??梢杂眠@一的方式解決,非常好用:?#define FUNC() do{func1(); func2();}while(0)
02. 數(shù)組的初始化
假如給arr的第2~6元素初始化為5,也許你會?int arr[10] = {0, 5, 5, 5, 5, 5, 0, 0, 0, 0};現(xiàn)在告訴你C99可以這樣:?int arr[10] = {[1... 5] = 5};03. 數(shù)組的訪問
你想取數(shù)組的第6個元素(下標為5),教科書教你這樣做:?int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};?int n1 = arr[5];
?int n2 = *(arr 5);其實你可以:?int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
?int n = 5[arr];也不會有錯,實際上arr[5]對應
*(arr 5)
,而5[arr]對應*(5 arr)
,沒多大區(qū)別。04. 結(jié)構(gòu)體的初始化
結(jié)構(gòu)體的初始化,傳統(tǒng)的做法是:?typedef struct?{
? ? ?int a;
? ? ?int x;
? ? ?int y;
? ? ?int z;
? ? ?char b;
? ? ?short c;
?}S;
?S s = {100, 0, 0, 0, 'A', 0x12);對于C99,其實你可以:?typedef struct
?{
? ? ?int a;
? ? ?int x;
? ? ?int y;
? ? ?int z;
? ? ?char b;
? ? ?short c;
?}S;
?S s = {
? ? ? ? ? ? .a = 100,
? ? ? ? ? ? .b = 'A',
? ? ? ? ? ? .c = 0x12
? ? ? ? };
05. 用include
的方式初始化大數(shù)組
?double array[SIZE][SIZE] = {? ? ?#include "float_values.txt"
?}
06. Debug時輸出文件名、函數(shù)名、行號等
?#define DEBUG_INFO() fprintf(stderr,"[DEBUG]%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);07. C語言有-->
“趨向于...”操作符?
?int main(void)?{
? ? ? ? ?int n = 10;
? ? ? ? ?while(n --> 0 ) // n goes to 0
? ? ? ? {
? ? ? ? ? ? ? ? ?printf("%d ", n);
? ? ? ? }
? ? ? ? ?printf("\n");
?}實際上C語言沒有這個
-->
操作符,是--
和>
的組合而已? ? ? ? ?while( n-- ?> ?0 )08. 獲得任意類型數(shù)組的元素數(shù)目
?#define NUM_OF(arr) (sizeof (arr) / sizeof (*arr))09. 判斷運行環(huán)境的大小端
Linux有以下代碼:? ? ?static union {? ? ? ? ?char c[4];
? ? ? ? ?unsigned long l;
? ? } endian_test = { { 'l', '?', '?', 'b' } };
? ? ?#define ENDIANNESS ((char)endian_test.l)
?
? ? ?printf("ENDIANNESS: %c\n", ENDIANNESS);
10. 編譯時做條件檢查
Linux Kernel有以下代碼?/* Force a compilation error if condition is true */?#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))例如,在某些平臺為了防止內(nèi)存對齊問題,檢查一個結(jié)構(gòu)體或者一個數(shù)組的大小是否為8的倍數(shù)。?BUILD_BUG_ON((sizeof(struct mystruct) % 8) != 0);除了這個,還有?#define BUILD_BUG_ON_ZERO(e) (sizeof(struct{int : -!!(e);}))
?#define BUILD_BUG_ON_NULL(e) ((void*)sizeof(struct{int : -!!(e);}))
?#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
?#define MAYBE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
11. 用異或運算實現(xiàn)數(shù)據(jù)交換
交換倆變量數(shù)據(jù),一般做法是:?// 方法1?temp = a;
?a = b;
?b = temp;
?
?// 方法2
?a=a b;
?b=a-b;
?a=a-b;方法1需要第三個變量,方法二存在數(shù)據(jù)溢出可能,可以嘗試下以下方法:a = a ^ b;
b = a ^ b;
a = a ^ b;
12. 判斷語句中把const
數(shù)值放在前面
通常條件語句寫成if(n == 0){ /*...*/ }但是,有可能手誤寫成if(n = 0){ /*...*/ }這種錯誤只有機器在運行時候知道,而人不一定能發(fā)現(xiàn)這種bug。把數(shù)值放在前面就不怕了,==
寫成=
,編譯器就知道if(0 == n){ /*...*/ }13. 用冒號表達式替代if...else...
語句
這個用法應該很普遍了,不算什么特別的技巧了。?if(y < 0)?{
? ? ?x = 10;
?}
?else
?{
? ? ?x = 20;
?}可以改成以下一行代碼即可?x = (y < 0) ? 10 : 20;