简体   繁体   English

C main函数是可选的,您可以在其外部编写代码吗?

[英]Is the C main function optional and can you write code outside of it?

Often times when I look at source code on GitHub the main function is omitted or there is code outside of it. 通常,当我在GitHub上查看源代码时, main功能会被忽略或在其外部有代码。 The tutorials I have done told me - I can do neither of these things. 我完成的教程告诉我-我不能做这些事情。 Is there something I am missing? 我有什么想念的吗?

Normal complete C programs hosted in an operating system must have a main routine. 操作系统中托管的常规完整C程序必须具有main例程。

Projects on GitHub may be mere portions of programs, such as a collection of routines intended to be used in other programs. GitHub上的项目可能只是程序的一部分,例如打算在其他程序中使用的例程的集合。 (Such collections are often called libraries.) If source code is not intended to be a complete program by itself, then it does not need to have a main routine. (这样的集合通常被称为库)。如果源代码不打算以自己是一个完整的程序,那么它并不需要有一个main例程。 A main routine will be added later, by somebody who uses the collection of routines in their own program. 稍后,将在自己的程序中使用例程集合的人添加一个main例程。

C source code can also be compiled and used in combination with source code written in other programming languages. C源代码也可以编译并与其他编程语言编写的源代码结合使用。 The behavior of this is not specified by the C standard, so it is specific to the various developer tools used when doing this. C标准未指定其行为,因此它特定于执行此操作时使用的各种开发人员工具。 Such a hybrid program must have some main routine, but it may be called something other than main . 这样的混合程序必须具有一些主例程,但是可以将其称为main以外的其他东西。 Nonetheless, main has become very popular as the name of the main routine, so it is used very frequently. 尽管如此, main作为main例程的名称已变得非常流行,因此使用非常频繁。

C source code can be used for special software, such as operating system kernels. C源代码可用于特殊软件,例如操作系统内核。 The C standard describes a freestanding environment, in contrast to a hosted environment. 与托管环境相比,C标准描述了一个独立的环境。 In a freestanding environment, many things are customized to the specific system, including how the starting address of the program is set. 在独立的环境中,许多事情都是针对特定系统定制的,包括如何设置程序的起始地址。 In this case, the main entry point might be called start instead of main , for example, and the address of that entry point might be conveyed to the hardware in some special data structure particular to the hardware. 在这种情况下,例如,主入口点可能被称为start而不是main ,并且该入口点的地址可能会以某种特定于硬件的特殊数据结构传送到硬件。

Regarding code outside of functions, that may be initialization expressions. 关于函数之外的代码,可以是初始化表达式。 (There are strict limits on what expressions can be used in initialization outside of functions. You cannot write general C code in those expressions.) You would have to show specific examples to get answers about that. (对于在函数外部的初始化中可以使用哪些表达式有严格的限制。您不能在这些表达式中编写通用的C代码。)您必须显示特定的示例才能获得有关此问题的答案。

by definition an EXECUTABBLE binary has a main method that is application's entry point. 根据定义,可执行二进制文件的主要方法是应用程序的入口点。

LIBRARIES (or rather anything that doesnt need to be executable by the OS doesnt have to have a main function 库(或者,不需要操作系统执行的任何内容都不必具有主要功能

so C Code itself can very well live without a main function. 因此,如果没有主要功能,C代码本身可以很好地运行。 You cant put arbitrary code outside of a function though (be it main or otherwise) .. 但是,您不能将任意代码放在函数之外(无论是主函数还是其他函数)..


generally you could say: 通常您可以说:

  • 'code has to live in a function in C' (the exception are variables and macros) “代码必须存在于C语言的函数中”(变量和宏除外)
  • the main function is the OSs entry point for executing the binary 主要功能是执行二进制文件的OS入口点

[thats a bit simplifed but a good rule of thumb IMO] [这有点简化,但IMO是一个好的经验法则]

Such samples simply are not complete, for a program to work there has to be an entry point (for standard C this is main). 这些样本根本不完整,要使程序正常运行,必须有一个入口点(对于标准C,这是主要的)。 Code statements have to be inside a function though that function need not be main. 代码语句必须在函数内部,尽管该函数不必是主要函数。

It is, however, possible to have variables with initializers outside function bodies. 但是,可以在函数主体外部使用带有初始值设定项的变量。

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

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