2

I would like to know how the memory is allocated to #define variables in C.

#define VAR1 10

I have 2 questions...

  1. What's the type of VAR1?
  2. In which memory segment VAR1 is stored?
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Sagar Jain
  • 7,475
  • 12
  • 47
  • 83
  • It's just like text replacement in text of code) – Ivan Ivanov Jul 07 '15 at 06:27
  • 1
    VAR1 is of type macro. It is not stored in any memory segment. It exists at compile time, and may not even be used, and if it is used, then its value will normally appear as a literal integer in the code that the compiler sees — and the compiler proper doesn't ever see VAR1. (Abnormally, it might be converted to a string by being an argument to a macro that stringizes its argument (`#` operator), or it might be incorporated into an identifier by a macro using token pasting (`##` operator).) – Jonathan Leffler Jul 07 '15 at 06:29

4 Answers4

7

In which memory segment VAR1 is stored?

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 of VAR1 are replaced with 10 so there is no memory requirement at run time because 10 is an integer literal.

What's the type of VAR1?

VAR1 is replaced with 10 at pre-processing stage. 10 being an integer literal, we can say type or VAR1 is int.


Moral: Macros are not variables.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
6

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.

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
2

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.

Codor
  • 17,447
  • 9
  • 29
  • 56
-1

Macros are not variables. They are just a common name for some value. In your case, VAR1 corresponds to integer value 10.

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 into Object code. During this conversion, the preprocessors are processed i.e. the header files are included, any external linked file is included and all the macro are replaced with their corresponding values (in your case, at any place the program finds VAR1, it will replace that with value 10).

After this phase, all the code has already been converted to nearly machine level code.

I hope you got your answer.

amulya349
  • 1,210
  • 2
  • 17
  • 26
  • 2
    Although correct about `VAR1` getting replaced, the rest of this answer is highly inaccurate to not say wrong. – alk Apr 26 '16 at 08:26
  • @alk : I would like to know how its inaccurate. Can you tell me in detail ? – amulya349 May 11 '16 at 06:17
  • 2
    The order of processing-steps of a C source file is different than you write. See for example here: http://stackoverflow.com/a/27031348/694576 – alk May 11 '16 at 11:20