What is the difference between main
in a C program and in a C++ program?
C程序中的main和c++程序的区别是什么?
Other than
除了
- return statement ( default 1 in C,`0 in C++)
- 返回语句(C默认为1,c++默认为0)
-
syntax:
语法:
int main() { /* … */ } int main(int argc, char* argv[]) { /* … */ } int main() , void main() ,etc ...
Mainly:
主要是:
-
difference between main in C Program & C++ program
C程序和c++程序中主程序的不同
-
Are there any differences between C++98, C++03 and C++0x according to the ISO standard? i.e program's entry point (program startup implementation), etc.
根据ISO标准,c++ 98、c++ 03和c++ 0x有什么不同吗?我。e程序的入口点(程序启动实现)等。
4 个解决方案
#1
10
In modern C, and modern C++:
在现代C和现代c++中:
-
main
is always eitherint main()
orint main(int, char*[])
. - main总是int main()或int main(int, char*[])。
- In C89, you have to
return
from main explicitly. - 在C89中,必须显式地从main返回。
- In C99 and C++, if you don't return explicitly, you implicitly
return 0
. - 在C99和c++中,如果不显式返回,则隐式返回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. 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", e.g. not for the Linux kernel); see 3.6.1. [/edit] To the best of my knowledge, calling conventions are also not part of the standard.
对于你的第二个问题,在C99中,你必须有两个主要功能之一。在c++中,标准说如果一个程序有一个返回int的主函数,并且每个符合标准的实现都必须接受两个列出的版本作为入口点(对于“托管程序”,例如对于Linux内核);3.6.1见。[/编辑]就我所知,打电话习惯也不是标准的一部分。
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显式地添加了一个内存模型,以便启用定义良好的并发和原子操作。
#2
8
In C, as opposed to C++, main
can be called recursively.
在C语言中,可以递归地调用main,而不是c++。
/* valid C */
#include <stdio.h>
int main(int argc, char **argv) {
putchar(argc ? '.' : '\n');
if (argc == 0) return 0;
return main(argc - 1, NULL);
}
#3
1
C99 and C++ are put in line for the definition of main
in hosted environments. There are two function interfaces that are allowed
C99和c++用于定义托管环境中的main。允许有两个函数接口
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而不返回语句,在这种情况下,EXIT_SUCCESS的返回值返回给调用者。
#4
1
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
. However, there is a huge difference in what happens before main is called in C versus C++. In C++, objects with static
storage are typically initialized prior to entering main
.
不是主要的。然而,在main在C和c++中调用之前发生的事情有很大的不同。在c++中,具有静态存储的对象通常在进入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. I've never run across an implementation that takes advantage of this flexibility.
注意:允许实现在main中执行静态数据的动态初始化,但必须在第一次引用静态数据之前执行。我从未遇到过利用这种灵活性的实现。
#1
10
In modern C, and modern C++:
在现代C和现代c++中:
-
main
is always eitherint main()
orint main(int, char*[])
. - main总是int main()或int main(int, char*[])。
- In C89, you have to
return
from main explicitly. - 在C89中,必须显式地从main返回。
- In C99 and C++, if you don't return explicitly, you implicitly
return 0
. - 在C99和c++中,如果不显式返回,则隐式返回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. 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", e.g. not for the Linux kernel); see 3.6.1. [/edit] To the best of my knowledge, calling conventions are also not part of the standard.
对于你的第二个问题,在C99中,你必须有两个主要功能之一。在c++中,标准说如果一个程序有一个返回int的主函数,并且每个符合标准的实现都必须接受两个列出的版本作为入口点(对于“托管程序”,例如对于Linux内核);3.6.1见。[/编辑]就我所知,打电话习惯也不是标准的一部分。
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显式地添加了一个内存模型,以便启用定义良好的并发和原子操作。
#2
8
In C, as opposed to C++, main
can be called recursively.
在C语言中,可以递归地调用main,而不是c++。
/* valid C */
#include <stdio.h>
int main(int argc, char **argv) {
putchar(argc ? '.' : '\n');
if (argc == 0) return 0;
return main(argc - 1, NULL);
}
#3
1
C99 and C++ are put in line for the definition of main
in hosted environments. There are two function interfaces that are allowed
C99和c++用于定义托管环境中的main。允许有两个函数接口
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而不返回语句,在这种情况下,EXIT_SUCCESS的返回值返回给调用者。
#4
1
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
. However, there is a huge difference in what happens before main is called in C versus C++. In C++, objects with static
storage are typically initialized prior to entering main
.
不是主要的。然而,在main在C和c++中调用之前发生的事情有很大的不同。在c++中,具有静态存储的对象通常在进入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. I've never run across an implementation that takes advantage of this flexibility.
注意:允许实现在main中执行静态数据的动态初始化,但必须在第一次引用静态数据之前执行。我从未遇到过利用这种灵活性的实现。