什么使用这些C的主要功能指示? [英] What these C's main function indicates?

查看:177
本文介绍了什么使用这些C的主要功能指示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/5296163/why-is-the-type-of-the-main-function-in-c-and-c-left-to-the-user-to-define\">Why在C和c的主要功能++留给用户来定义的类型?


我遇到许多C的主要功能风格和写作的语法,但我没有真正得到什么此语法意味着任何人可以给每个语法简单?
为什么无效?为什么中断?为什么作废,诠释为参数?

 无效的主要(){}诠释主(){}诠释主要(无效){}无效的主要(无效){}诠释主(INT){}INT主(INT ARGC,CHAR *的argv []){}


解决方案

有两种形式由C标准规定符合实现:


  • 实施托管和放大器;

  • 独立式实施

有基于两类envrionments的是,C标准定义为:


  • 托管环境和放大器;

  • 分别独立式环境。

什么是独立环境&安培;什么是托管环境

一个独立的实现是一个专为那些没有操作系统的好处执行的程序。
对于防爆:一个操作系统内核或嵌入式环境将是一个独立的环境

使用的操作系统的设备的程序通常会在托管的实现。

如何做一个C程序在这两个环境中执行?有什么区别?

如何在C程序在这两个环境不同开始执行。结果
对于独立式环境中,程序启动时可通过任何实现定义的函数发生。 还有,即使是的main()应该存在没有要求。

因此​​,任何在问题中提到的功能定义,可根据实施情况的独立环境中有效。它们的功能参数和返回值将实现定义的含义,所以,你将需要检查自己的文件,以了解他们的precise含义。

参考:


  

5.1.2.1独立环境


  
  

在独立环境中(其中C程序的执行可能发生,没有任何
  一个操作系统的利益),的的名称和功能的类型,称为在程序
  启动是实现定义
。可供独立的图书馆设施
  程序,不是由第4条要求的最小集合等,都是实现定义的。


对于托管环境标准的任务程序的执行由的main()函数开始执行,这还要求怎么这个函数将被定义。

有相同的规格中给出:


  

C99标准:5.1.2.2托管环境


  
  

5.1.2.2.1程序启动


  
  

1名为在程序启动时的功能被命名为主力。实施没有声明
  原型此功能。它应带有int和返回类型来定义不
  参数:

  INT主要(无效){/ * ... * /}


  
  

或具有两个参数(此处称为argc和argv,虽然任何名称可能是
  使用,因为它们是局部的,其中声明它们的功能):

  INT主(INT ARGC,CHAR *的argv []){/ * ... * /}

或同等学历;或在一些其它实现定义方式。


Possible Duplicate:
Why is the type of the main function in C and c++ left to the user to define?

I've come across many C's main function style and syntax of writing but I'm not actually getting what this syntax mean can anybody give brief on each syntax? why void? why int? why void,int as parameter?

void main() {

}

int main() {

}

int main(void) {

}

void main(void) {

}

int main(int) {

}

int main(int argc, char* argv[]) {

}

解决方案

There are two forms of conforming implementations specified by the c standard:

  • Hosted Implementation &
  • Freestanding Implementation

There are based on two types of envrionments that the c standard defines as:

  • Hosted environment &
  • Freestanding environment respectively.

What is freestanding Environment & What is Hosted Environment?

A freestanding implementation is one that is designed for programs that are executed without the benefit of an operating system. For Ex: An OS kernel or Embedded environment would be a freestanding environment.

A program using the facilities of an operating system would normally be in a hosted implementation.

How does a c program execute in these two environments? What is the difference?

How a C program begins execution in both these environment differs.
For an Freestanding environment, the program startup can happen by any implementation defined function. There is no requirement that even a main() should exist.

So any of the functions definitions mentioned in the question can be valid depending upon implementation for that Freestanding Environment. And their function parameters and return values will have implementation defined meaning, So you will need to check their documentation to know their precise meanings.

Reference:

5.1.2.1 Freestanding 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. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.

For an Hosted environment the standard mandates the program execution begins by execution of a main() function and it also mandates how this function will be defined.

The specifications for the same are given in:

C99 Standard: 5.1.2.2 Hosted environment

5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

   int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

   int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.

这篇关于什么使用这些C的主要功能指示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆