When to use #define or const in C? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

When to use #define or const in C?

The #define directive uses no memory and is used by the preprocessor to manipulate the source code before compilation. The const function is used to prevent the source code from changing the value of a certain identifier, but in doing so it requires memory. Under what circumstances do programmers use these?

16th Jan 2022, 7:31 PM
Harrison
Harrison  - avatar
3 Answers
+ 4
One simple idea I use is to use *const* everywhere and use #define only where I can't use *const*, like while defining header guards. As in C ( unlike C++ ), *const* qualified types are not "true" constant expressions, so you will find a lot more cases where you might want to define a macro over *const*
17th Jan 2022, 2:28 AM
Arsenic
Arsenic - avatar
16th Jan 2022, 9:08 PM
Coding Cat
Coding Cat - avatar
+ 3
https://www.includehelp.com/cpp-tutorial/difference-between-const-and-define.aspx https://stackoverflow.com/questions/1674032/static-const-vs-define-vs-enum
17th Jan 2022, 6:46 AM
Ipang