Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a variable name stored in C?

I want to ask how C the variables are stored in C?

To be more clear consider the following code:

int main() {
    int a = 1, b;
    b = a + 2;
    return 0;
}

For example here in what memory C stores the names of variable places.

eg if &a=0x12A7(suppose) &b=0x123B1, then how and where does c stores the variable names like in which memory name a is stored?

like image 744
Nimit Bhardwaj Avatar asked Dec 20 '15 14:12

Nimit Bhardwaj


People also ask

How are variable names stored?

The variables are stored in a symbol table. It also contains the set of bits or bytes required or allocated to the variable ie the memory address. Example: A=41.

Where are variable values stored in C?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

What is variables name in C?

Where type is one of C types (such as int ), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign a value to the variable.

How are data variables stored?

How is a variable stored in a computer memory ? int x = 15; Typically the computer in its ram allocates a 4 bytes chunk of memory, storing the value 15 in the form of 0's and 1's in the allocated 4 bytes and x refers to the address of the 4 bytes allocated memory.


2 Answers

Variable names need not be stored at all! The compiler can get rid of them entirely. Imagine, if the compiler is quite clever, it can reduce your entire program to this:

int main(){
  return 0;
}

Note that the effect of this program is exactly the same as your original, and now there are no variables at all! No need to name them now, is there?

Even if the variables in your code were actually used, their names are purely a convenient notation when you write the program, but aren't needed by the processor when it executes your code. As far as a microprocessor is concerned, a function like this:

int foo(int x, int y) {
  int z = x + y;
  return z * 2;
}

Might result in compiled code that does this, in some hypothetical simple instruction set architecture (ISA):

ADD # consumes top two values on stack (x and y), pushes result (z)
PUSH 2 # pushes 2 on stack
MULT # consumes top two values on stack (z and 2), pushes result
RET

The longer story is that variable names are sometimes stored for debugging purposes. For example if you're using GCC you can pass the -g option to emit a "symbol table" which contains things like variable names for debugging. But it isn't needed simply to run a program, and it isn't covered by the language standard--it's an implementation feature which differs by platform.

like image 95
John Zwinck Avatar answered Oct 17 '22 04:10

John Zwinck


C doesn't store name of the variables. Its the compiler that stores the names of variables in compiler's symbol table.
This data structure is created and maintained by compiler.
An example of a symbol table for the snippet

// Declare an external function
extern double bar(double x);

// Define a public function
double foo(int count)
{
    double  sum = 0.0;

    // Sum all the values bar(1) to bar(count)
    for (int i = 1;  i <= count;  i++)
        sum += bar((double) i);
    return sum;
}  

may contain at least the following symbol:

enter image description here

like image 26
haccks Avatar answered Oct 17 '22 05:10

haccks