Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the 'main' function located? [closed]

Tags:

c

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).

like image 461
merry-go-round Avatar asked Dec 14 '17 17:12

merry-go-round


People also ask

Does the main function go on top or bottom?

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.

Where is main function declared?

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.

Where is the main function stored in C?

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.

What is the function of main () function?

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.


1 Answers

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.

like image 110
P.P Avatar answered Sep 28 '22 12:09

P.P