Sunday, May 10, 2009

What is difference between #define and const keyword in c language?

frends can any body tell me #define and const in c language regarding their usage and practice.

What is difference between #define and const keyword in c language?
# define is a preprocessor directive which tells the compiler to replace the variable with the string before compilation.





Const is a normal variable whose value can't be changed.
Reply:The const keyword declares a "read-only variable" if you will, whereas the #define keyword is a compiler directive, and can be used to define more than just values. For instance





#define INCX x++;





is a valid definition, which could be used as follows:





int main


{


int x=0;


INCX


return x;


}





This is a valid program, will compile, and will return the value 1. This is a pointless example, but I think it should help explain the difference.





Rawlyn.


No comments:

Post a Comment