为什么 main 前面有一个 int ,为什么我的教授会排除它? [英] Why does main have an int in front of it and why could my Professor be excluding it?

查看:27
本文介绍了为什么 main 前面有一个 int ,为什么我的教授会排除它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在我教授的幻灯片中,他只是举例:

So in my professor's slides he simply gives examples like:

main()
{...
}

对吧?但是当我把它放在 Visual Studio 中时,它会出错,当我将 int 放在 main 前面时它会起作用.为什么我的教授不把 int 放在 main 前面?main 可以是任何其他类型的变量吗?我也看到很多 int main(void).为什么这是必要的?其他的可以作为参数输入吗?

Right? But when I put this in visual studio it gives an error and it works when I put int in front of main. Why would my professor not put int in front of main? Can main be any other type of variable? Also I see a lot of int main(void). Why is this necessary? Can anything else be put in as a parameter?

推荐答案

main 返回 int.在旧版本的 C 中,您可以省略 int,编译器会假装您说的是 int.在 C++ 中,如果 'main' 没有明确返回值,它会神奇地返回 0.您可以从 main 返回三个值:0、EXIT_SUCCESS 和 EXIT_FAILURE.0 相当于 EXIT_SUCCESS.这两个命名值在 中定义,如果您在 中使用 C++ 编码.

main returns int. In older versions of C you could leave out the int and the compiler would pretend that you had said int. In C++, if 'main' doesn't explicitly return a value, it magically returns 0. You can return three values from main: 0, EXIT_SUCCESS, and EXIT_FAILURE. 0 is equivalent to EXIT_SUCCESS. The two named values are defined in <stdlib.h> and if you're coding in C++ in <cstdlib>.

void 是一个 C 风格的声明,函数不接受任何参数.在 C++ 中你不需要它;声明中没有参数的函数不接受任何参数.

The void is a C-style declaration that a function takes no arguments. In C++ you don't need it; a function that has no arguments in its declaration takes no arguments.

一般来说,main 需要两个参数:

In general, though, main takes two arguments:

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

那些是命令行参数.argc 是参数的数量,而 argv 是指向保存参数的 C 样式字符串的指针数组.第一个字符串 (argv[0]) 是程序的名称.

Those are the command-line arguments. argc is the number of arguments, and argv is an array of pointers to C-style strings that hold the arguments. The first string (argv[0]) is the name of the program.

这篇关于为什么 main 前面有一个 int ,为什么我的教授会排除它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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