+ 5

What is the difference between #define and const in C?

" #define " is a preprocessor directive to define macros that can be used through out your code. And you can also define constants with it. On the other hand, " const " is a keyword that is used to declare constants. So, why both?

24th Feb 2017, 6:31 AM
Logan
Logan - avatar
2 odpowiedzi
+ 4
#define is used for exact replacements. #define ABC 2+2 ... some_variable = ABC*ABC; printf("%d",some_variable); ... Output is not 4*4 = 16. It is 2+2*2+2 = 8. And, 'const' is used only to store a single value.
24th Feb 2017, 12:13 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 2
i think its the same.. just whether you prefer to define it or using const
24th Feb 2017, 9:50 AM
kausper?QIANG?
kausper?QIANG? - avatar