-
Refugee
#define
What is #define? I've seen people use it like #include but what does #define do?
Thanks
-Chris
-
hi,
#define is a preprocessor directive. In a simple term it is some thing like keyword which hints the preprocessor (the earlier phase of compilation.)
The preprocessor replaces the string which is defined with #define with its value.
That is
#define OK 1
if(m_answer == OK)
{
}
than OK will be replaced by 1 before compilation. Thus you can make your program more readable and understandable by using descriptive strings for numbers.
Also #define is used to define a macro. The macro is a small piece of code which is used more than once in the program.
Before compilation this code will be placed wherever this macro has been called.
#define SQURE(x) x*x
b=SQURE(3); // code
This code will be converted automatically to
b=3*3; //before compilation
If you make the function for this purpose. The compiler will generated many more instructions for this small function(to do sequencing purpose) , which will be negative for performance of the program.
But make sure that don't use macro for more than 2 to 3 statments of code because it doesn't make sense in terms of programming style.
Refer any C book which has covered Preprocessor directives chapter. You will get many more help for that..
Bye..
jatan ([email protected])
-