0

What's the difference between #define and using 'const' for constant in c language?

26th Apr 2020, 10:14 AM
Shibin Rasheed
Shibin Rasheed - avatar
3 Antworten
+ 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.
26th Apr 2020, 10:28 AM
Mustafa K.
Mustafa K. - avatar
+ 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.
26th Apr 2020, 2:56 PM
Nitish Kumar
Nitish Kumar - avatar
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
12th May 2020, 3:28 AM
Amal Gil
Amal Gil - avatar