Search code examples
ccompiler-construction

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 stores the variable names like in which memory name a is stored?


Solution

  • 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