C语言 参数列表为 void 的 main 与参数列表为空的 main 不同吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13876868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:44:23  来源:igfitidea点击:

Is main with parameter list of void different from main with an empty parameter list?

cmainvoid

提问by Joseph Lee

Possible Duplicate:
Why is the type of the main function in C and c++ left to the user to define?

可能的重复:
为什么 C 和 C++ 中主函数的类型留给用户定义?

What is a void? Anyone provide some examples, proper use of void? And what is the difference when we write void main (void)or main()?

什么是?任何人都提供一些例子,正确使用void?当我们写void main (void)或时有什么区别main()

回答by Omkant

In C, in general, (void)means no arguments required in function call, while ()means unspecified number of arguments.

在 C 中,通常(void)意味着函数调用中不需要参数,而()意味着未指定数量的参数。

e.g.

例如

void foo(void)
{
   // body
}

void bar()
{
    //body
}

In calling enviroment,

在调用环境中,

foo();  // Correct 
foo(1); // Incorrect
bar();  // Correct
bar(1); // Also correct

This was the general explanation.

这是一般的解释。

But for your case for main() , C99 Standard says that,

但是对于 main() 的情况,C99 标准说,

5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: int main(void) { /* ... */ }

5.1.2.2.1 程序启动

程序启动时调用的函数名为 main。实现声明没有此函数的原型。它应定义为返回类型为 int 且不带参数: int main(void) { /* ... */ }

or

或者

with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): int main(int argc, char *argv[]) { /* ... */ }or equivalent;

带有两个参数(此处称为 argc 和 argv,尽管可以使用任何名称,因为它们对于声明它们的函数而言是局部的): int main(int argc, char *argv[]) { /* ... */ }或等效的;

or

或者

in some other implementation-defined manner.

以其他一些实现定义的方式。

So, in this void main(void)return type should be int.

所以,在这个void main(void)返回类型中应该是int.

And at last , for main(), return type is not given so implicitly return type would be int.

最后,对于main(),没有给出返回类型,所以隐式返回类型将是int

回答by lunadir

Excluding the return type of the main as in

排除 main 的返回类型,如

main(){
}

doesn't mean that it's a voidtype, it depends on the compiler. I think it can be said it's generically interpreted as

并不意味着它是一种void类型,它取决于编译器。我认为可以说它一般被解释为

int main(){
}

The voidtype tells the compiler that there is no 'entity' (no 'storage'), so

void类型告诉编译器不存在“实体”(无“存储装置”),所以

void func(int i)

takes an int but returns nothing. In the case of parameters this:

接受一个 int 但什么都不返回。在参数的情况下:

void func()

is equivalent to this:

相当于:

void func(void)

which indicates more explicitly that it does not take parameters. Different story is with the type void *which isa type, a pointer to something dimensionless.

这更明确地表明它不接受参数。不同的故事是类型void *,它一个类型,一个指向无量纲的指针。

回答by Ravi

Basically, voidis a data type, which basically used with method declaration. It means nothingor no type. Eg:

基本上,void是一种数据类型,基本上与方法声明一起使用。这意味着没有没有类型。例如:

1) int myFunc(void)-- the function takes nothing.

1) int myFunc(void)-- 该函数什么都不做。

2) void myFunc(int)-- the function returns nothing

2) void myFunc(int)-- 函数什么都不返回

3) void* data;-- 'data' is a pointer to data of unknown type, and cannot be dereferenced

3) void* data;-- 'data' 是一个指向未知类型数据的指针,不能解引用

回答by Kevin

Void means "emptyness". In your example of void main() it means that the functions main() does not return a value. I feel obliged tell you that void main() should be avoided (no pun intended) at all costs, use int main() instead. int main() makes sure your program can return a value of type int to the OS on close. There are numerous other uses of void, check out this websiteif you want to read more about this.

空的意思是“空”。在您的 void main() 示例中,这意味着函数 main() 不返回值。我觉得有必要告诉你应该不惜一切代价避免 void main() (没有双关语意),而是使用 int main() 。int main() 确保您的程序可以在关闭时向操作系统返回一个 int 类型的值。void 还有许多其他用途,如果您想了解更多信息,请查看此网站

回答by John Bode

voidis a data type with no values. It is also an incompletedata type that cannot be completed. When used as a return type for a function, it indicates that the function does not return a value:

void是一种没有值的数据类型。它也是一种无法完成的不完整数据类型。当用作函数的返回类型时,表示该函数不返回值:

void foo(int x);

When used as a parameter list, it indicates that the function takes no arguments:

当用作参数列表时,表示该函数不带参数:

void bar(void);

This is different from an empty parameter list, which indicates that the function takes an unspecifiednumber of arguments (in C; in C++, an empty parameter list is the same as using void):

这与空参数列表不同,空参数列表表示函数采用未指定数量的参数(在 C 中;在 C++ 中,空参数列表与 using 相同void):

void bletch();

No object (variable) can be typed void. However, you can declare pointers of type void *: these are "generic" pointers, and can be converted to and from other pointer types without an explicit cast. The standard memory allocation functions malloc, calloc, and reallocall return void *:

不能输入任何对象(变量)void。但是,您可以声明 type 的指针void *:这些是“通用”指针,可以在没有显式强制转换的情况下与其他指针类型相互转换。标准内存分配函数malloccallocrealloc一切回归void *

double *darr = malloc(sizeof *darr * rows);

In a hosted implementation (basically, anything with an operating system), mainmust be declared as

在托管实现中(基本上,任何带有操作系统的东西),main必须声明为

int main(void)

or

或者

int main(int argc, char **argv) // the parameter names can be whatever you want,
                                // but argc and argv are the usual convention;
                                // char **argv is equivalent to char *argv[]

or in some other implementation-defined manner; an implementation mayaccept

或以其他一些实现定义的方式;一个实现可以接受

void main()

as a legitimate signature for main, but it must explicitly document that somewhere.

作为 的合法签名main但它必须在某处明确记录该签名。