Does a macro allocate memory in a segment? If yes, in which memory segment NUMBER
is stored?
#define NUMBER 10
Does a macro allocate memory in a segment? If yes, in which memory segment NUMBER
is stored?
#define NUMBER 10
No, macros don't work like that.
They are substituted by the preprocessor. The compiler doesn't get to see NUMBER
. All it will see is the literal 10
.
In this respect, C and C++ are identical.
No memory is allocated for the macro. When compiling the code, the macro is mechanically replaced with its value in all places it is used.
No, Macro does not allocate the memory.
These statements are not like variable assignment, no memory is allocated. Remember, the preprocessor acts before compilation.
NUMBER
is relevant only in pre-processing
stage and does not have any identity at run time. During pre-processing all instances of NUMBER
are replaced with 10. so there is no memory requirement at run time because 10 is an integer literal(Compile time).
Please, read reference link.
No, in general, a #define
directive does not allocate any memory as such.
To elaborate, #define
is a replacement directive. All it does is a textual replacement during the preprocessing stage.
So, a textual code like
#define NUMBER 10
.
.
.
if (val == NUMBER)
looks like
if (val == 10)
after preprocessing. So, the MACRO substitution defined by #define
does not take any separate memory. FWIW, The replacement (literal) value will take up usual place in the code segment (same as if you've written the later format, with direct litreal value, without a MCARO).
Related, from C11, chapter §5.1.1.2/ p4, Translation phases
Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed. [...]
This define does not allocate memory in your program.
It just tells the preprocessor (part of the compiler) to replace further instances of the token NUMBER
by 10
.
It probably allocates some bytes in the preprocessor, though.
A #define
by itself does not take up any memory, what happens is the compiler - during pre-compilation stage - replaces all occurrences of NUMBER in your code with the definition, in your case 10.