Where is the 'main
' function located?
int main() {
const char *str = "hello world";
printf("%s\n", str);
}
I know about these locations:
.text
.data
.bss
stack
heap
I think that the answer is .bss
because I know 'main
' function is not located in others (but I might be wrong).
The usual convention is to put them at the end of the script. And of course you can't call main before you define it.
The main() function is where your source code starts. As part of the C compiler there are a also include files such as #include <stdio. h> and others. These include files contain declarations for the functions of the C Standard Library such as the printf() function.
Call stack stores the information of active subroutines of a computer program. Since, Main() function is already defined by compiler.So,it should be stored in a datastructure similar to call stack.
The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.
This is obviously outside the realms of C standard because it doesn't mention any "sections".
Using ELF systems as an example, typically all the code will be in text segment (also known as code segment) and main
will be in text segment. You can look at where the symbols reside by using readelf
or objdump
commands. For example, if you inspect the output of objdump -S a.out
you can see assembly and various sections.
You can read http://wiki.osdev.org/ELF and http://www.cs.cmu.edu/afs/cs/academic/class/15213-f00/docs/elf.pdf for further info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With