Saturday, November 8, 2008

C - FAQs

C - FAQ s
Q1. Which is better to use - #define or const?

Ans:
# define abc 5
This #define has global effect. #define is a preprocessor directive which simply so text replacement. In place of abc, 5 is substituted. We cannot make #define local to a function.
Next is const int n=5;
Here n is an integer with value 5. Const is placed in the read only section of the memory. This const qualifier can have local effect. It can be local to a function. Once the const int n is initialized with value 5, it is not possible to change the value of n. Changing the value of n result in compile time error.
It is possible to change the value of ‘abc’ after ‘undef’ .
Eg 1. #define abc 5
#define abc 10
This results in error.
Eg 2. Consider another scenario.
#define abc 5undef abc
#define abc 10
This code will wok fine.

No comments: