C语言 void 是 C 中的数据类型吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3487689/
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
Is void a data type in C?
提问by suhel
Is voida data type in the C programming language? If so, what type of values can it store? If we have int, float, char, etc., to store values, why is voidneeded? And what is the range of void?
是voidC 编程语言中的数据类型吗?如果是这样,它可以存储什么类型的值?如果我们有int, float,char等来存储值,为什么void需要?而虚空的范围是多少?
回答by James Curran
Void is considered a data type (for organizational purposes), but it is basically a keyword to use as a placeholder where you would put a data type, to represent "no data".
Void 被认为是一种数据类型(出于组织目的),但它基本上是一个关键字,用作放置数据类型的占位符,以表示“无数据”。
Hence, you can declare a routine which does not return a value as:
因此,您可以将不返回值的例程声明为:
void MyRoutine();
But, you cannot declare a variable like this:
但是,您不能像这样声明变量:
void bad_variable;
However, when used as a pointer, then it has a different meaning:
但是,当用作指针时,则具有不同的含义:
void* vague_pointer;
This declares a pointer, but without specifying which data type it is pointing to.
这声明了一个指针,但没有指定它指向的数据类型。
回答by Keith Thompson
Yes, voidis a type. Whether it's a datatype depends on how you define that term; the C standard doesn't.
是的,void是一种类型。它是否是数据类型取决于您如何定义该术语;C 标准没有。
The standard does define the term "object type". In C99 and earlier; voidis not an object type; in C11, it is. In all versions of the standard, voidis an incomplete type. What changed in C11 is that incomplete types are now a subset of object types; this is just a change in terminology. (The other kind of type is a function type.)
该标准确实定义了术语“对象类型”。在 C99 及更早版本中;void不是对象类型;在 C11 中,它是。在标准的所有版本中,void都是不完整的类型。C11 中的变化是不完整类型现在是对象类型的子集;这只是术语的变化。(另一种类型是函数类型。)
C99 6.2.6 paragraph 19 says:
C99 6.2.6 第 19 段说:
The voidtype comprises an empty set of values; it is an incomplete type that cannot be completed.
所述空隙类型包括一个空的一组值; 它是无法完成的不完整类型。
The C11 standard changes the wording slightly:
C11 标准稍微改变了措辞:
The voidtype comprises an empty set of values; it is an incomplete object type that cannot be completed.
所述空隙类型包括一个空的一组值; 它是无法完成的不完整对象类型。
This reflects C11's change in the definition of "object type" to include incomplete types; it doesn't really change anything about the nature of type void.
这反映了 C11 在“对象类型”的定义中的变化,以包括不完整的类型;它并没有真正改变 type 的性质void。
The voidkeyword can also be used in some other contexts:
该void关键字还可以在其他一些上下文中使用:
As the only parameter type in a function prototype, as in
int func(void), it indicates that the function has no parameters. (C++ uses empty parentheses for this, but they mean something else in C.)As the return type of a function, as in
void func(int n), it indicates that the function returns no result.void*is a pointer type that doesn't specify what it points to.
作为函数原型中唯一的参数类型,如 中所示
int func(void),表示该函数没有参数。(C++ 为此使用空括号,但它们在 C 中表示其他含义。)作为函数的返回类型,如 中所示
void func(int n),表示该函数不返回任何结果。void*是一个指针类型,它没有指定它指向什么。
In principle, all of these uses refer to the typevoid, but you can also think of them as just special syntax that happens to use the same keyword.
原则上,所有这些用法都是指typevoid,但您也可以将它们视为恰好使用相同关键字的特殊语法。
回答by Jens
The C Standard says that voidis an incomplete type that cannot be completed (unlike other incomplete types that can be completed). This means you cannot apply the sizeofoperator to void, but you can have a pointer to an incomplete type.
C 标准说这void是无法完成的不完整类型(与其他可以完成的不完整类型不同)。这意味着您不能将sizeof运算符应用于void,但您可以拥有指向不完整类型的指针。

