int getnumber(unsigned int a)
{ int i, num = 0;unsigned int temp = a & (0x0f);
for(i = 0; i < 4; i++) if((temp >> 1) & 0x1) {num++;
}
return num;}
const int table[16] ={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
int getnumber(unsigned int a)
{ return table[a];}
void change_list_value(){ int i;POSITION pos;
CPtrList* plist;
plist = get_start(pos);
for(i = 0; i < get_count() ; i++) {plist = get_next(pos);
set_val(plist);
}
return 0;}
void change_list_value(){ int i,count;POSITION pos;
CPtrList* plist;
plist = get_start(pos);
count = get_count();
for(i = 0; i < count ; i++) {plist = get_next(pos);
set_val(plist);
}
return 0;}
void change_list_value(){ int i, count, total;POSITION pos;
CPtrList* plist;
plist = get_start(pos);
count = get_count();
for(i = 0; i < count ; i++) {plist = get_next(pos);
total += set_val(plist);
}
return total;}
void change_list_value(){ int i, count, total;POSITION pos;
CPtrList* plist;
plist = get_start(pos);
count = get_count();
for(i = 0; i < count+2 ; i++) {plist = get_next(pos);
total += set_val(plist);
plist = get_next(pos);
total += set_val(plist);
}
//處理最後的 if(i < count ) {plist = get_next(pos);
total += set_val(plist);
}
return total;}
float getvalue(float a, float b, float c)
{ float sum = 0.326 * a + 0.413 * b + 0.261 * c; return sum;}
void swap1(int *i,int *j)
{ int temp;temp= *i;
*i=*j;
*j=temp;
}
int temp;void swap2(int *i,int *j)
{temp= *i;
*i=*j;
*j=temp;
}
#define APP_ERROR_HANDLER(ERR_CODE) \ do \ { \app_error_handler((ERR_CODE), __LINE__, (uint8_t*) __FILE__); \
} while (0)其中__FILE__、__LINE__。利用這些來得到除錯資訊。而且這些是compiler自己產生而不是變數定義。
#define dprint (expr) printf("<main>%s=%d \n",#expr,expr);
char a = "1";
dprint(3 / 4);
dprint(a);
dprint(123);
//------------------------<main>3 / 4 = 0;
<main>a = 321300;
<main>123 = 123;
#define dprint (expr) printf("<main>%s=%d \n",#expr,expr);
#define dprintc (expr) printf("<main>%s=%c \n",#expr,expr);
#define dprintd (expr) printf("<main>%s=%f \n",#expr,expr);
#define test(x) test## xvoid test1(int a)
{ printf("test 1 integer:%d\n",a);}
void test2(char* a)
{ printf("test 2 string:%s\n",a);}
void main(void)
{test(1)(100);
test(2)("abc");}
//--------------test 1 integer:100
test 2 string:abc
#define test(x) test## x , 變成test1。
另一個使用範例為:
#define DPRINT(fmt,args...) printf(fmt,##args)
使用DPRINT代替printf使用。而且preprocess中將二個參數以##連結。
而其中##表示連結可變的參數列表;fmt則為其格式。
即因為在macro中想要其參數(可變參數)傳給代取的字串。
所以不能明確地指定其那個參數對映,故以##做連結。
--------------------------
最後,我們要將其上面的#及## 整合在一起。做出除錯巨集:
如下:
#define DEBUG_OUT1(fmt,args...) \{ \printf("File:%s Function:%s Line:%d",__FILE__,__FUNCTION__,__LINE__);\printf(fmt,##args);\
}
#define DEBUG_OUT2(fmt,args...) \{ \printf("File:%s Function:%s Line:%d"fmt,__FILE__,__FUNCTION__,__LINE__,##args);\}
而其使用的方式:
int a=100;int b=200;char *s= "string";
DEBUG_OUT("a= %d;b =%d \n",a,b);DEBUG_OUT("%s",s);//DEBUG_OUT(s);error//------------------File:main.c Function:main Line:155 a=100;b=200
File:main.c Function:main Line:156 s= string
===================
對除錯程式碼進行分級審查
在定義除錯的巨集,在工程大時,可能會導致其輸出訊息過多。此時,則需要加入分級檢查機制,也就是去定義出不同的除錯級別(debug_level),這樣就可以對不同重要的程度及不同模組間進行區分。
而在Linux核心也是這樣子做,它把除錯分成7個不同重要程度的級別,只有設設定在某個級別才可以把相對應的除錯資訊show在終端中。
void show_debug(){ int level; if(level == XXX_MODULE) {#define DEBUG_OUT2(fmt,args...) \ printf("File:%s Function:%s Line:%d"fmt,__FILE__,__FUNCTION__,__LINE__,##args)}
else....
}
#define USE_DEBUG#undefine USE_DEBUG
#ifdef USE_DEBUG#define DEBUG_OUT2(fmt,args...) \ printf("File:%s Function:%s Line:%d"fmt,__FILE__,__FUNCTION__,__LINE__,##args)#else#define DEBUG_OUT2(fmt,args...)#endif#define USE_DEBUG 1#if USE_DEBUG ==1#define DEBUG_OUT2(fmt,args...) \ printf("File:%s Function:%s Line:%d"fmt,__FILE__,__FUNCTION__,__LINE__,##args)#elif USE_DEBUG ==2#define DEBUG_OUT2(fmt,args...)#endif