Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

When I create a variable, how does the system know the memory location corresponding to it?

For example, in C if I write:

#include <stdio.h>

int main(){

int a=10;

printf("%d",a);

return 0;

}

how does the computer know the memory address of the variable a?

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    When the program is compiled all the static variables you create are allocated memory by the compiler

    So, take the line...

    int a = 10;

    The compiler creates a memory location big enough to hold an integer and uses that address to store the number 10 in.

    When you print it out on the next line it prints the contents of the memory location you called 'a'

    High level languages like C are so people don't have to manually write all their code in assembly language

    Source(s): This will explain things a bit more thoroughly http://pw1.netcom.com/~tjensen/ptr/ch1x.htm
  • ?
    Lv 7
    1 decade ago

    Part of the compiler's job is a table of variables; type, addresses. Once you compile the code, the table is no longer needed because the machine code has the address where ever it is needed.

Still have questions? Get your answers by asking now.