I would like to know how the memory is allocated to #define variables in C.
#define VAR1 10
I have 2 questions...
- What's the type of VAR1?
- In which memory segment VAR1 is stored?
I would like to know how the memory is allocated to #define variables in C.
#define VAR1 10
I have 2 questions...
Macros
arenot variables
. They are just a common name for some value. In your case,VAR1
corresponds tointeger
value10
.Macro is not stored anywhere in the memory. When we compile the program in C or C++, it is done in many stages. First, the syntax is checked. If syntax is correct, it is checked for semantic errors. If it passes then, the
.c
program file is converted intoObject code
. During this conversion, the preprocessors are processed i.e. the header files are included, any external linked file is included andall the macro are replaced with their corresponding values
(in your case, at any place the program findsVAR1
, it will replace that with value10
).After this phase, all the code has already been converted to nearly machine level code.
I hope you got your answer.
VAR1
has neither a type nor any runtime representation. It's only recognized by the preprocessor.So the answer is Mu: your question cannot be answered because it is based on incorrect assumptions.
In none of the segment.
VAR1
is relevant only in pre-processing stage and does not have any identity at run time. During pre-processing all instances ofVAR1
are replaced with10
so there is no memory requirement at run time because10
is an integer literal.VAR1
is replaced with10
at pre-processing stage.10
being aninteger literal
, we can say type orVAR1
isint
.Moral: Macros are not variables.
To my understanding, a definition via a macro does neither have a type nor explicitly allocates memory; the right-hand side of the definition (
10
in this case) is expanded textually into any occurence of the left-hand side (VAR1
in this case) before the compilation.