简体   繁体   English

C&C ++中main的区别是什么

[英]What is the difference between main in C & C++

What is the difference between main in a C program and in a C++ program? C程序中的main和C ++程序之间有什么区别?

Other than 以外

  1. return statement ( default 1 in C,`0 in C++) return语句(C中默认为1,C ++中为0)
  2. syntax: 句法:

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

Mainly: 主要是:

  1. difference between main in C Program & C++ program C程序和C ++程序中的main之间的区别

  2. Are there any differences between C++98, C++03 and C++0x according to the ISO standard? 根据ISO标准,C ++ 98,C ++ 03和C ++ 0x之间有什么区别吗? ie program's entry point (program startup implementation), etc. 即程序的入口点(程序启动实现)等。

In modern C, and modern C++: 在现代C和现代C ++中:

  • main is always either int main() or int main(int, char*[]) . main总是int main()int main(int, char*[])
  • In C89, you have to return from main explicitly. 在C89中,您必须明确地从main return
  • In C99 and C++, if you don't return explicitly, you implicitly return 0 . 在C99和C ++中,如果未显式return 0 ,则隐式return 0

[ (I've checked the C99 standard now and edited this paragraph.) ] For your second question, in C99 you must have precisely one of the two main functions. [ (我现在已经检查了C99标准并编辑了这一段。) ]对于第二个问题,在C99中你必须正好具备两个main功能之一。 In C++ the standard says that a program is well-formed if it has a main function that returns int , and that every conforming implementation must accept the two listed versions as an entry point (for a "hosted program", eg not for the Linux kernel); 在C ++中,标准表示如果程序具有返回intmain函数,并且每个符合要求的实现必须接受两个列出的版本作为入口点(对于“托管程序”,例如不适用于Linux),程序就是格式良好的。核心); see 3.6.1. 见3.6.1。 [/edit] To the best of my knowledge, calling conventions are also not part of the standard. [/ edit]据我所知, 调用约定也不是标准的一部分。

I don't understand your question about memory, but do note that neither C99 nor C++03 have anything but a rudimentary memory model, whereas the new C++0x explicitly adds a memory model in order to enable well-defined concurrent and atomic operations. 我不明白你关于内存的问题,但要注意C99和C ++ 03都没有任何东西,只有一个基本的内存模型,而新的C ++ 0x显式添加了一个内存模型,以便能够定义良好的并发和原子操作。

In C, as opposed to C++, main can be called recursively. 在C中,与C ++相反, main可以递归调用。

/* valid C */
#include <stdio.h>
int main(int argc, char **argv) {
  putchar(argc ? '.' : '\n');
  if (argc == 0) return 0;
  return main(argc - 1, NULL);
}

C99 and C++ are put in line for the definition of main in hosted environments. C99和C ++用于托管环境中main的定义。 There are two function interfaces that are allowed 允许有两个功能接口

int main(void);
int main(int, char*[]);

Both languages allow the implicit return from main without return statement in which case a return value of EXIT_SUCCESS is returned to the caller. 两种语言都允许从main return隐式返回而不return语句,在这种情况下,返回值EXIT_SUCCESS返回给调用者。

Edit: Is there any difference in program startup implementation is there any difference in c++98,C++03,C+++0x main ,etc......... 编辑:程序启动实现有什么区别c ++ 98,C ++ 03,C +++ 0x main等有什么区别.........

Not in main . 不是main However, there is a huge difference in what happens before main is called in C versus C++. 然而,在使用C与C ++调用main 之前发生的事情存在巨大差异。 In C++, objects with static storage are typically initialized prior to entering main . 在C ++中,具有static存储的对象通常在进入main之前初始化。

Note: 注意:
An implementation is allowed to perform dynamic initializations of static data in the midst of main , but it must do so prior to the first reference to that static data. 允许实现在main中间执行静态数据的动态初始化,但必须在第一次引用该静态数据之前执行此操作。 I've never run across an implementation that takes advantage of this flexibility. 我从未遇到过利用这种灵活性的实现。

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

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