In your example, var2
is simply text to be replaced by the C preprocessor. Before the program is even compiled, the preprocessor parses the source file and replaces any occurrences of "var2" (that aren't inside comments or string literals) with the expansion text, which in your example is the number 200
. It doesn't occupy space in memory because the compiler doesn't even see the "var2", it just sees the numerical constant literal "200".
var1
, however, is an unmodifiable lvalue (i.e. it does exist in memory). When you qualify a variable definition with the keyword const
, the compiler will throw an error any time the program attempts to modify the variable.
Using macros like #define var2 200
does have performance benefits - namely your program will take less memory (because there are less variables on the stack) and your program may run a bit (maybe negligibly) faster (because the processor doesn't have to always load values in from memory).
However, macros can be error-prone and, except in the most performance-intense applications, the safeguards of using const
variables usually outweigh the costs. Still, today's compilers are very smart and can perform optimizations for const
variables to even further mitigate the performance costs.