Is a main() required for a C program?

后端 未结 7 472
后悔当初
后悔当初 2020-12-15 03:13

Well the title says it all. Is a main() function absolutely essential for a C program?

I am asking this because I was looking at the Linux kernel code,

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 03:58

    No, the ISO C standard states that a main function is only required for a hosted environment (such as one with an underlying OS).

    For a freestanding environment like an embedded system (or an operating system itself), it's implementation defined. From C99 5.1.2:

    Two execution environments are defined: freestanding and hosted. In both cases, program startup occurs when a designated C function is called by the execution environment.

    In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined.

    As to how Linux itself starts, the start point for the Linux kernel is start_kernel though, for a more complete picture of the entire boot process, you should start here.

提交回复
热议问题