Define is completely understandable and the typedef stands for aliases. Listen to the same meaning, the 2 is the difference?
Let's start with a simple example to see basic usage.
//define and typedef differences#defineDB Double//Replace the content replaced by define replaced contenttypedefDoubledb//alias typedef original type name new alias//typedef need a semicolon, typeof remove the following grammatical rules, define remove will be error voidMain () {//I can't see the difference .DB D1 =1.2, d2=1.8;//equivalent double d1 = 1.2,d2=1.8;DB D3 =1.9, d4=2.3;//equivalent Double d3 = 1.9,d4=2.3;printf ("%f,%f", D1,D2); printf ("\n%f,%f", D3,D4); }
The only difference is that the typedef needs semicolons.
Test the difference of 2 by pointer variables
#defineDP Double *typedefDouble*DP;voidmain2 () {DP dp1,dp2;//DP is substituted, equivalent double *dp1,dp2;//DP1 is a pointer to 4 bytes, DP2 is a double type data 8 bytesDP DP3,DP4;//are 4-byte pointers, equivalent to Dobule *DP3,*DP4;printf ("%d,%d",sizeof(DP1),sizeof(DP2));//4,8printf"\n%d,%d",sizeof(DP1),sizeof(DP2));//bis}
As you can see from the above example, define is completely replaced, and TypeDef is different.
Finally, the use of define and typedef to construct function pointers is described.
1 voidPrintintnum)2 {3printf"Time%d", num);4 }5 6 voidMain ()7 {8 //function Pointers9 void (*p) (int num) = print;Ten P (ten); One}
But it's annoying to write as much as the 9th line of code above, and you can use define and typedef to build
1 //typedef int* Pint;//give int type pointer alias2typedefvoid(*p) (intNUM);//to the function pointer type alias, p is the alias of the type3 #definePprint (X) void (*x) (int num)//macro mode with Parameters4 5 voidMain ()6 {7 //function Pointers8 9 //using typedefTenP P1 =Print; OneP1 (Ten); A - //using define -Pprint (P2) =Print; theP2 ( -); -}
C language Define and typedef differences and usage