Reputation: 747
I am trying to understand what does exactly happen at compiler level when I declare a variable/function? I tried finding it online. But I am only getting information about difference between declaration vs definition.
Upvotes: 0
Views: 96
Reputation:
The compiler essentially reserves an identifier and takes note of different characteristics such as the data type, the extent of the program area (scope) where is it "known" and other attributes such as lifetime, accessibility, namespace... This information is entered in a table.
From then on, references to this identifier in the same scope are interpreted in accordance with the declaration and checked for consistency. If no declaration is seen before an identifier is used, the compiler will issue an error message and try a definition on its own to let the compilation continue anyway.
Somewhere in the program, the same entity must be defined. In addition to what a declaration does, a definition will "embody" the identifier by mapping some storage space to it and assign it an initial value. This additional information is required by the linker. If it is missing, the compiler will keep silent, as the program is syntactically valid. Only the linker will complain.
Upvotes: 0
Reputation: 310957
The compiler enters a variable-definition row into an appropriately scoped symbol table, which contains the name, type, modifiers, ...
Upvotes: 2