| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1178 人关注过本帖
标题:请解释一下void main() 和int main()的区别。新手见笑了。
只看楼主 加入收藏
朔源
Rank: 1
等 级:新手上路
帖 子:105
专家分:4
注 册:2015-9-22
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:8 
请解释一下void main() 和int main()的区别。新手见笑了。
主函数返回类型的概念。我看的是谭浩强的    C语言程序设计   里面没有讲main()函数的返回类型。网上找的看的也是云里雾里的。请帮我讲一下。
我是新手。自学的。写程序时为什么有时没有写返回类型可以。有时候又有警告。。
void main()和int main()有什么区别。两种类型都是在什么情况下用的。主函数还有其他类型吗?
静候!解惑!不胜感激!
搜索更多相关主题的帖子: 不胜感激 程序设计 C语言 网上 
2015-10-08 19:22
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
收藏
得分:1 
这个以前论坛里发了很多,搜一下吧

一片落叶掉进了回忆的流年。
2015-10-08 19:56
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:10 
别想太复杂了,就简单的去理解字面意思就行了
void main()//没有返回值

int main() //有返回值

前者为经典C入口
如:MS C
后者为标准C入口
如:ANSI C/ISO C
2015-10-08 19:58
·charles
Rank: 2
等 级:论坛游民
帖 子:67
专家分:48
注 册:2015-3-23
收藏
得分:2 
用void 就在函数最后不加return,用int就在函数最后加return 整型

编程!编程!!编程!!!
重要的事情说三遍!!!!
2015-10-09 01:05
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:3 
不管是C,还是C++;不管是远古,还是现代。
都只有两种标准的模式
int main(void) { /* ... */ }

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

虽然标准中允许“some other implementation-defined manner”,但绝不是就允许你瞎搞,而是某些平台上可能需要更多的信息,比如windows上私有的 int main( int argc, char *argv[], char *envp[] ) 形式。

总之,你给出的两种形式都是错误的
void main() 返回类型必须是 int
int main() 在C语言中(在C++中不是这样)也就是 int main(...),这是不可以的
收到的鲜花
  • pycansi2015-10-09 11:19 送鲜花  10朵   附言:详细,专业
2015-10-09 08:58
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:2 
摘自《C++ Primer Plus 6th edition》
The Function Header as an Interface
Right now the main point to remember is that C++ syntax requires you to begin the
definition of the main() function with this header: int main().This chapter discusses the
function header syntax in more detail later, in the section “Functions,” but for those who
can’t put their curiosity on hold, here’s a preview.
In general, a C++ function is activated, or called, by another function, and the function
header describes the interface between a function and the function that calls it.The part
preceding the function name is called the function return type; it describes information flow
from a function back to the function that calls it.The part within the parentheses following
the function name is called the argument list or parameter list; it describes information
flow from the calling function to the called function.This general description is a bit confusing
when you apply it to main() because you normally don’t call main() from other
parts of your program.Typically, however, main() is called by startup code that the compiler
adds to your program to mediate between the program and the operating system
(Unix,Windows 7, Linux, or whatever). In effect, the function header describes the interface
between main() and the operating system.
Consider the interface description for main(), beginning with the int part.A C++
function called by another function can return a value to the activating (calling) function.
That value is called a return value. In this case, main() can return an integer value, as indicated
by the keyword int. Next, note the empty parentheses. In general, a C++ function
can pass information to another function when it calls that function.The portion of the
function header enclosed in parentheses describes that information. In this case, the empty
parentheses mean that the main() function takes no information, or in the usual terminology,
main() takes no arguments. (To say that main() takes no arguments doesn’t mean
that main() is an unreasonable, authoritarian function. Instead, argument is the term computer
buffs use to refer to information passed from one function to another.)
In short, the following function header states that the main() function returns an integer
value to the function that calls it and that main() takes no information from the function
that calls it:
int main()
Many existing programs use the classic C function header instead:
main() // original C style
Under classic C, omitting the return type is the same as saying that the function is type
int. However, C++ has phased out that usage.
You can also use this variant:
int main(void) // very explicit style
Using the keyword void in the parentheses is an explicit way of saying that the function
takes no arguments. Under C++ (but not C), leaving the parentheses empty is the
same as using void in the parentheses. (In C, leaving the parentheses empty means you are
remaining silent about whether there are arguments.)

Some programmers use this header and omit the return statement:
void main()
This is logically consistent because a void return type means the function doesn’t
return a value. However, although this variant works on some systems, it’s not part of the
C++ Standard.Thus, on other systems it fails. So you should avoid this form and use the
C++ Standard form; it doesn’t require that much more effort to do it right.
Finally, the ISO C++ Standard makes a concession to those who complain about the
tiresome necessity of having to place a return statement at the end of main(). If the compiler
reaches the end of main() without encountering a return statement, the effect will
be the same as if you ended main() with this statement:
return 0;
This implicit return is provided only for main() and not for any other function

PS:
a,注意红色部分,int main()和int main(void)
C和c++是有区别的
C语言:int main(void)和int main()是有区别的
C++: int main(void)等于int main()
b,r版主说的是对的,标准C只有
int main(void) { /* ... */ }

int main(int argc, char *argv[]) { /* ... */ }
这两种写法
2015-10-09 13:07
朔源
Rank: 1
等 级:新手上路
帖 子:105
专家分:4
注 册:2015-9-22
收藏
得分:0 
多谢各位。。
2015-10-09 15:21
the_second
Rank: 2
等 级:论坛游民
帖 子:115
专家分:80
注 册:2015-9-13
收藏
得分:2 
我个人的理解
main 是主函数
void main 是不用返回值的
int main 是要返回 return 0;的
2015-10-09 15:46
朔源
Rank: 1
等 级:新手上路
帖 子:105
专家分:4
注 册:2015-9-22
收藏
得分:0 
void main()和int main()。void是不向主函数返回值,int 是向主函数返回值。意思知道。
只是这两个各在什么情况下用。用不好了有什么问题不是很清楚
2015-10-09 16:27
快速回复:请解释一下void main() 和int main()的区别。新手见笑了。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018138 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved