简体   繁体   English

程序执行总是从C中的main开始吗?

[英]Does the program execution always start from main in C?

Must program execution start from main, or can the starting address be modified? 必须从main开始执行程序,还是可以修改起始地址?

#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 . 这个程序in main之前打印in fun

The '#pragma' command is specified in the ANSI standard to have an arbitrary implementation-defined effect. “#pragma”命令在ANSI标准中指定,具有任意实现定义的效果。 In the GNU C preprocessor, '#pragma' first attempts to run the game 'rogue'; 在GNU C预处理器中,'#pragma'首先尝试运行游戏'流氓'; if that fails, it tries to run the game 'hack'; 如果失败,它会尝试运行游戏'hack'; if that fails, it tries to run GNU Emacs displaying the Tower of Hanoi; 如果失败了,它会尝试运行显示河内塔的GNU Emacs; 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 - Richard M. Stallman,GNU C预处理器,版本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. 这通常是一些名为_start汇编程序例程或者某些程序,位于(在Unix机器上)编译器程序包附带的文件crt0.o中。 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, ie running their constructors). 它执行运行C可执行文件所需的设置(比如,设置stdinstdoutstderratexit()使用的向量...对于C ++,它还包括初始化全局对象,即运行它们的构造函数)。 Only then does control jump to main() . 只有这样才能控制跳转到main()

As the quote at the beginning of my answer expresses so eloquently, what #pragma does is completely up to your compiler. 由于我的答案开头的引用如此雄辩地表达, #pragma所做的完全取决于你的编译器。 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...) (我猜你的pragma startup - 应该在#前面加上#告诉运行时首先调用fun() ...)

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 . 就ISO C标准而言, 对于托管实现, C程序的入口点始终是main (除非使用某些实现定义的功能来覆盖它)。 For a "freestanding implementation" (typically an embedded system, often with no operating system), the entry point is implementation-defined. 对于“独立实现”(通常是嵌入式系统,通常没有操作系统),入口点是实现定义的。

C programs are not necessarily start from main() function. C程序不一定从main()函数开始。 Some codes are executed before main() that zero out all uninitialized global variables and initialize other global variables with proper value. 一些代码在main()之前执行,它将所有未初始化的全局变量清零并用适当的值初始化其他全局变量。 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() . 在上面的示例代码中, ab分别在main()执行第一行之前分配010

The #pragma directive is there to define implementation-defined behaviour. #pragma指令用于定义实现定义的行为。 Your code with #pragma may compile in some compiler but may not compile in another. 使用#pragma代码可以在某些编译器中编译,但可能无法在另一个编译器中编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM