Deep discussion on the usage of MACRO
来源:互联网 发布:网络安全意识培训内容 编辑:程序博客网 时间:2024/05/01 09:54
see the code directly:
/* macro.c by vinco at 2011-09-15* i386-Red Hat-gcc-4.1.2 * http://blog.csdn.net/xuyunzhang/* Copyright: All Reserved*/#include<stdio.h>#include<stdlib.h>#include<string.h>#define conn(x,y) x##y//xy, (x)##(y) is not correct !!!#define toString(x) #x //"x", #(x) is not correct !!!#define SWAP_INT(x,y)({(x)^=(y)^=(x)^=(y);}) //can x,y be point !!!/* support both struct type and basic type, but not point */#define SWAP_GENERAL(x,y,DATA_TYPE)do{\DATA_TYPE tmp;\memcpy(&tmp, &x, sizeof(DATA_TYPE) );\memcpy(&x, &y, sizeof(DATA_TYPE) );\memcpy(&y, &tmp, sizeof(DATA_TYPE) );\}while(0)/* support only point/address !!! */#define SWAP_POINT(x,y,DATA_TYPE)do{\DATA_TYPE tmp;\memcpy(&tmp, x, sizeof(DATA_TYPE) );\memcpy(x, y, sizeof(DATA_TYPE) );\memcpy(y, &tmp, sizeof(DATA_TYPE) );\}while(0)typedef struct classmate{int age;char name[30];}ClassMate;void FuncTest(void);int main(void){/*-----------------------------------------------*/int num= conn(123,456);//int num = (123##456);char* pStr = toString(vinco zhang);//char* p = #vinco;printf("num = %d\n", num);printf("pStr = %s\n", pStr);putchar('\n');/*-----------------------------------------------*/int x =10,y=20;printf("x=%d, y=%d\n", x, y);SWAP_INT(x,y);//SWAP_GENERAL( x, y, int); // is ok too !!!printf("x=%d, y=%d\n", x, y);putchar('\n');/*-----------------------------------------------*/int *px =&x,*py=&y;printf("*px=%d, *py=%d\n", *px, *py);//SWAP_INT( px, py); // error of course !!!//SWAP_INT( *px, *py); //not correct, but why ???//SWAP_GENERAL( *px, *py, int); // is ok too !!!SWAP_POINT( px, py, int);printf("*px=%d, *py=%d\n", *px, *py);putchar('\n');/*-----------------------------------------------*/ClassMate classmates[2] ={{124, "vinco" },{24, "zhang" }};printf("classmates[0].age = %d, classmates[0].name = %s\n", classmates[0].age, classmates[0].name );printf("classmates[1].age = %d, classmates[1].name = %s\n", classmates[1].age, classmates[1].name );SWAP_GENERAL(classmates[0], classmates[1], ClassMate); //SWAP_POINT(&classmates[0], &classmates[1], ClassMate); //is ok too !!!printf("classmates[0].age = %d, classmates[0].name = %s\n", classmates[0].age, classmates[0].name );printf("classmates[1].age = %d, classmates[1].name = %s\n", classmates[1].age, classmates[1].name );putchar('\n');/*-----------------------------------------------*/ClassMate* pClassmates[2] ={&classmates[0],&classmates[1]};printf("pClassmates[0]->age = %d, pClassmates[0]->name = %s\n", pClassmates[0]->age, pClassmates[0]->name );printf("pClassmates[1].age = %d, pClassmates[1]->name = %s\n", pClassmates[1]->age, pClassmates[1]->name );//SWAP_GENERAL( *pClassmates[0], *pClassmates[1], ClassMate); //is ok too !!!SWAP_POINT( pClassmates[0], pClassmates[1], ClassMate);printf("pClassmates[0]->age = %d, pClassmates[0]->name = %s\n", pClassmates[0]->age, pClassmates[0]->name );printf("pClassmates[1]->age = %d, pClassmates[1]->name = %s\n", pClassmates[1]->age, pClassmates[1]->name );putchar('\n');/*-----------------------------------------------*/FuncTest();/*-----------------------------------------------*/return 0;}void FuncTest(void){printf( "The %s is %s\n", toString(__FUNCTION__), __FUNCTION__ );printf( "The %s is %s\n", toString(__FILE__), __FILE__ );printf( "The %s is %d\n", toString(__LINE__), __LINE__ );printf( "The %s is %s\n", toString(__DATE__), __DATE__ );printf( "The %s is %s\n", toString(__TIME__), __TIME__ );}
make and run it:
[vinco@IPPBX-Server ctest]$ make macrocc macro.c -o macro[vinco@IPPBX-Server ctest]$ ./macronum = 123456pStr = vinco zhangx=10, y=20x=20, y=10*px=20, *py=10*px=10, *py=20classmates[0].age = 124, classmates[0].name = vincoclassmates[1].age = 24, classmates[1].name = zhangclassmates[0].age = 24, classmates[0].name = zhangclassmates[1].age = 124, classmates[1].name = vincopClassmates[0]->age = 24, pClassmates[0]->name = zhangpClassmates[1].age = 124, pClassmates[1]->name = vincopClassmates[0]->age = 124, pClassmates[0]->name = vincopClassmates[1]->age = 24, pClassmates[1]->name = zhangThe __FUNCTION__ is FuncTestThe __FILE__ is macro.cThe __LINE__ is 111The __DATE__ is Sep 15 2011The __TIME__ is 17:36:13[vinco@IPPBX-Server ctest]$
1. " #define toChar(x)#@x //'x' "
it's not support in gcc, at least in Red Hat-gcc-4.1.2 and Ubuntu-gcc-4.3.2
however, it's supported in Visual C++ 6.0 .
2.
#define conn(x,y) (x)##(y) //xy,
#define toString(x) #(x) //"x",
are not correct !!!
3. some syntax is only suitable for macro defination, it accur error once used in general place.
int i = (123##456);
char* p = #vinco;
are error !!!
4. the value of a specific macro is determined once complete compiled, otherwise compile it again.
so the value of "__DATE__" and "__DATE__" are not changed even although execute the programm many times.
5. macro is really a good skill you can take for, especially the macro with parameter.
because it merely do some about substitution, the paremeter can be the type itself,
such as "char", "struct ClassMate".
just see "SWAP_GENERAL(x,y,DATA_TYPE)".
6. GCC support "{ any; specific; context; }" now!!! , it's equivalent to "do{any; specific; context;}while(0)",
any more , you can distribute it to several lines just by adding "\" to the end per line.
(no space charater followed any more, including ' ','\t')
- Deep discussion on the usage of MACRO
- A Brief Discussion on”Chinese-Style of Crossing The Road”
- the usage of vi on mac
- Deep discussion on method of judging a system whether a little endian or big endian
- The Hopes of Agnes Grey Contrasted with the Reality ---- Discussion on the Distance between Ideality and Reality
- The usage of finalize()
- usage of the serialVersionUID
- the usage of lucene
- The usage of SQLPATH
- the usage of sscanf()
- the usage of vim
- The usage of #pragma
- The usage of InjectView
- Usage of the vc timer
- The Usage of map::insert
- The Usage of Java Timer
- the usage of keyword const
- the usage of android viewpage
- html5支持的语义化标签
- Android学习笔记(十九)——SeekBar与RatingBar
- chrome对锚记的处理
- C语言常见排序算法
- 记录:nginx proxy_pass转php给apache
- Deep discussion on the usage of MACRO
- js 去除空格
- C#如何为winform程序打包发布应用(图解教程)
- Junit
- 如何安装 Android SDK 和Eclipse 插件
- 数据结构的静态查找(C语言实现)
- 网站压力测试工具
- ASP.net动态加载JS代码与CSS文件
- 【转】自定义ListView FastScroller滑块图片