0
What's the difference between #define and using 'const' for constant in c language?
3 Answers
+ 4
When you declare constant by const, it takes space in memory but define just creates a synonym, for example instead of typing 12 you just use the name you created for that number.
+ 1
#define is a preprocessor directive which do not occupy space in memory while const is a function which occupies the some memory.
The work of both are same to define a constant.
0
>>Constants are fixed value wich they are stored in memory .(memory allocation)
Before the compiler executes the code the preprocessor affect (send)the value when it reaches the constant name.
>>eg:
Int main(){
const int number= 3;
Printf("%d",number);//3
This constant is only accessible in the main function.
>>the define predective processor does not store the value in the the memory,but it search and insert(replace )the value.
>>eg:#define max 50
Int main(){
Int array[max]; //50.
The max value is accessible in all the file with all the functions.
Hope it is clear