Originally Posted by
Kittu20
This means that the compiler/linker allocates memory address for a global variable and assigns a value to it before the main function is executed.
It's the compiler that initializes x during the compile process. Look at the source file and the assembler code:
Source file:
Code:
int x = 20;
int main(void)
{
return 0;
}
Compile command: [gcc -s global.c]
Look at the section"x:"
Assembler output:
Code:
.file "global.c"
.text
.globl x
.data
.align 4
.type x, @object
.size x, 4
x:
.long 20
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Debian 12.2.0-14) 12.2.0"
.section .note.GNU-stack,"",@progbits
The linker would resolve the definition of the global variable, and it's use in main() if used.