C编程中的返回类型 [英] Return Types in C Programming

查看:80
本文介绍了C编程中的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经很久了,我还没有完全理解C中的返回类型和参数。

int main()

void main()

void main(void).... etc



这些声明实际意味着什么?我读到编译器将1或0返回给操作系统。

编译器与操作系统有什么关系?

为什么我们将参数传递给a功能毕竟?我们不能在函数内声明我们需要的东西吗?

一个清晰简洁的答案最受欢迎....

It''s been ages and I''m yet to understand return types and parameters completely in C.
int main()
void main()
void main(void) ....etc

What do these declarations actually mean? I read that that the compiler returns 1 or 0 to the operating system.
What does the compiler have to do with the operating system?
Why do we pass parameters to a function after all? Can''t we declare whatever we need inside that function?
A clear and concise answer is most appreciated ....

推荐答案

1。完成后,main()将返回一个int到O / S
2. main()将不返回O / S的值(不再是有效的C)

3. main()不接受任何变量并且不向O / S返回任何内容



4.编译器返回一个表示成功或错误的值。批处理文件和ShellScripts可以根据程序返回给操作系统的值做出决定。例如 - 如果编译阶段不成功,则尝试链接它是没有意义的。



5.因为任何有用的函数都可以在同一个函数中对不同的数据进行操作办法。比如printf - 你传递一个包含文本和格式信息的字符串,以及你想要格式化打印的数据。



或以乘法函数为例 - 只有你可以告诉它乘以哪些数字(输入变量)并且可以接收计算结果(返回值)
1. main() will return an int to the O/S when it finishes
2. main() will return no value to the O/S (no longer valid C)
3. main() takes no variables and returns nothing to the O/S

4. The compiler returns a value that indicates success or error. Batch files and ShellScripts can base decisions on the value returned to the OS by a program. E.g - if the compile phase is unsuccessful, there''s no point trying to link it.

5. Because any useful function can operate on different data in the same way. Take for instance printf - you pass it a string that includes text and formatting info, in addition to the data you''d like printed in a formatted manner.

Or take for instance a multiplication function - it''s only useful if you can tell it what numbers to multiply (input variables) and can receive the result of the calculation (return value)


If你使用的是默认的lib编译器选项,那么你的代码实际上会有额外的代码来调用main并处理内存分配,准备,安全cookie等等。当你从main返回后,操作系统将从程序中获取状态,该状态是从main返回的任何状态。



在Windows中,GetExitCodeProcess和%errorlevel%是看到这种情况的方式。



tl;如果你的应用程序没有正确完成,你可以通知用户和操作系统。
If you''re using a default lib compiler option, then your code will actually have additional code that calls main and handles memory allocation, preparation, security cookies, and etc. When that ends, after you''ve returned from main, the OS will get a status from the program, that status being whatever you return from main.

In windows, GetExitCodeProcess and %errorlevel% are ways of seeing this.

tl;dr you can notify the user and OS if your app did not complete correctly.


这篇关于C编程中的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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