Something interesting about the MACRO in C Programming
This Afternoon I continued the learning of the macro. Using the same PPT demonstrated during the class on Tuesday, I think I’ve get more:
So, I’d like to record something.
It is mainly about #define
.
#define
This will definitely make programming more efficient.
There are two basic types of macro:
#define MACRO String
This type of macro is quite simple, it just replace “Macro” with “String” in your code.
#define MACRO(argument1, argument2, …) String containing the arguments
This type of macro is more like a function.
But keep in mind that this is not a function, and what is does is still the replacement of strings in your code.
What’s more, users should beware of the returning value.
This version doesn’t have return values, and it is more safer:
#define _TEST(x,y) \
do{ \
(some codes containing x&y) \
}while(0)
Beware that no ;
in macros, and if you add a ;
, is will be worked into the replacement.
Others:
#
It turns the things behind into a string.
##
It connects the macros before and behind.
#@
It turns the thing behind into a character.