请问程序的执行总是从主要用C开始? [英] Does the program execution always start from main in C?
问题描述
必须编程从主执行开始,或者可以在起始地址被修改?
的#include<&stdio.h中GT;无效的乐趣();启动的#pragma乐趣诠释的main()
{
的printf(主);
返回0;
}无效的乐趣()
{
的printf(在乐趣);
}
此程序打印在乐趣
在主
。
在'的#pragma'命令在ANSI标准中规定有一个任意实现定义的效果。在GNU C preprocessor的#pragma'首先尝试运行游戏'流氓';如果失败,它会尝试运行游戏'黑客';如果失败,它会尝试运行GNU Emacs的显示汉诺塔方案;如果失败,它报告一个致命错误。在任何情况下,preprocessing不会继续。
块引用>- 理查德M. Stallman的,在GNU C preprocessor,版本1.34
开始执行程序在启动code或运行。这通常是
_start
或者叫一些汇编程序例程诸如此类,位于(在Unix机器上)在文件的crt0.o
的自带的编译器包。它运行一个C的可执行文件(如所需的设置,设立标准输入
,标准输出
和标准错误
,由的atexit()
...为C ++还包括初始化全局对象,即运行其构造函数)。只有这样做控制跳转到的main()
。由于我的答案当然presses开始的报价雄辩地,是什么
的#pragma
不完全取决于你的编译器。检查其文档。 (我猜你的编译启动
- 这应该是的方式以#
ppended $ P $ - 通知运行时调用乐趣()
第一...)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
beforein main
.解决方案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 filecrt0.o
that comes with the compiler package. It does the setup required to run a C executable (like, setting upstdin
,stdout
andstderr
, the vectors used byatexit()
... for C++ it also includes initializing of global objects, i.e. running their constructors). Only then does control jump tomain()
.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 yourpragma startup
- which should be prepended with a#
by the way - tells the runtime to callfun()
first...)这篇关于请问程序的执行总是从主要用C开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!