Does the program execution always start from main in C?

14,291

Solution 1

The '#pragma' command is specified in the ANSI standard to have an arbitrary implementation-defined effect. In the GNU C preprocessor, '#pragma' first attempts to run the game 'rogue'; if that fails, it tries to run the game 'hack'; if that fails, it tries to run GNU Emacs displaying the Tower of Hanoi; if that fails, it reports a fatal error. In any case, preprocessing does not continue.

-- Richard M. Stallman, The GNU C Preprocessor, version 1.34

Program execution starts at the startup code, or "runtime". This is usually some assembler routine called _start or somesuch, located (on Unix machines) in a file crt0.o that comes with the compiler package. It does the setup required to run a C executable (like, setting up stdin, stdout and stderr, the vectors used by atexit()... for C++ it also includes initializing of global objects, i.e. running their constructors). Only then does control jump to main().

As the quote at the beginning of my answer expresses so eloquently, what #pragma does is completely up to your compiler. Check its documentation. (I'd guess your pragma startup - which should be prepended with a # by the way - tells the runtime to call fun() first...)

Solution 2

As far as the ISO C Standard is concerned, the entry point for a C program is always main (unless some implementation-defined feature is used to override it) for a hosted implementation. For a "freestanding implementation" (typically an embedded system, often with no operating system), the entry point is implementation-defined.

Solution 3

C programs are not necessarily start from main() function. Some codes are executed before main() that zero out all uninitialized global variables and initialize other global variables with proper value. For example, consider the following code:

int a;
int b = 10;

int main()
{
    int c = a * b;
    return 0;
}

In the example code above, a and b are assigned 0 and 10 respectively before the execution of first line in main().

The #pragma directive is there to define implementation-defined behaviour. Your code with #pragma may compile in some compiler but may not compile in another.

Share:
14,291
desprado
Author by

desprado

Updated on June 14, 2022

Comments

  • desprado
    desprado almost 2 years

    Must program execution start from main, or can the starting address be modified?

    #include <stdio.h>
    
    void fun();
    
    #pragma startup fun
    
    int main()
    {
        printf("in main");
        return 0;
    }
    
    void fun()
    {
        printf("in fun");
    }
    

    This program prints in fun before in main.

  • DevSolar
    DevSolar over 12 years
    Isn't the setting up of .data and .bss sections done by the executable loader, instead of the startup code?
  • DevSolar
    DevSolar over 12 years
    Actually I wish compiler builders had stuck with Stallman's position. It would have saved us from stupid stuff like pragma once or source code hard-disabling compiler warnings...
  • Donotalo
    Donotalo over 12 years
    @devsolar: this may not be always the case. for example micro-controllers. I've worked on OKI 411, OKI 431 and PIC16F micros. They all have some sort of startup/runtime code. btw, i don't know how these are typcially achieved when programmed for PC. i've only heard of the names .data and .bss, don't know much about them. can you please give me some link from which i can learn more?
  • DevSolar
    DevSolar over 12 years
    Basic ELF object file layout: Section .text contains the machine code, .data contains initialization data (the 10 in your example). .bss is the "zero initialized" area, which of course is not in the executable, or rather only as a "size" number; you leave it to the loader to provide sufficient zero-initialized memory. Reality, of course, is more difficult (sections containing debug information, or C++ exception handling, or stuff like that). Check Wikipedia, "Data segment".