Home > front end >  Where are variable names stored in a C program?
Where are variable names stored in a C program?

Time:01-10

For example,I define a variable

int a=5;

I know the 5 is stored in the stack, but where is a stored? Please help

CodePudding user response:

Where are variable names stored in a C program?

The variable names are stored in the source code.

The details of the produced program are outside the scope of the language, but typically, the variable names are stored - if they are stored at all - in "debug information".

CodePudding user response:

Here is a complete minimal example that demonstrates @eerorika's answer about the debug information (here, in ELF files):

$ cat main.cpp
int main(int argc, char **argv)
{
    int whereIsMyVar = 5;
    return whereIsMyVar;
}

And compiling it without optimizations (-O0), and with debug info (-g):

$ g   -g -O0 main.cpp -o main

Now let's see what went on with objdump:

$ objdump -D -S main > main.asm
$ sed -n "378,389p" main.asm
00000000000005fa <main>:
int main(int argc, char **argv)
{
 5fa:   55                      push   %rbp
 5fb:   48 89 e5                mov    %rsp,%rbp
 5fe:   89 7d ec                mov               
  •  Tags:  
  • Related