C++ C中EOF和'\0'的值是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4705968/
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-08-28 16:12:31  来源:igfitidea点击:

What is value of EOF and '\0' in C

c++cnulleof

提问by theReverseFlick

I know that EOFand '\0'are of type integers, but if so shouldn't they have a fixed value?

我知道EOF并且'\0'是整数类型,但如果是这样,它们不应该有一个固定值吗?

I printed both and got -1 for EOFand 0 for '\0'. But are these values fixed?

我打印了两者并得到 -1 forEOF和 0 for '\0'。但是这些值是固定的吗?

I also had this

我也有这个

int a=-1;

printf("%d",a==EOF); //printed 1

Are the value for EOFand '\0'fixed integers?

EOF'\0'固定整数的值吗?

回答by CB Bailey

EOFis a macro which expands to an integer constant expression with type intand an implementation dependent negative value but is very commonly -1.

EOF是一个宏,它扩展为具有类型int和实现相关的负值的整数常量表达式,但通常为 -1。

'\0'is a charwith value 0 in C++ and an intwith the value 0 in C.

'\0'char在 C++ 中是值为 0 的,在 C 中int是值为 0 的。

The reason why printf("%d",a==EOF);resulted in 1was because you didn't assign the value EOFto a. Instead you checked if awas equal to EOFand since that was true (a == -1 == EOF) it printed 1.

printf("%d",a==EOF);导致的原因1是因为您没有将值分配EOFa. 相反,您检查是否a等于EOF并且因为它是 true( a == -1 == EOF) 它打印1

回答by Matteo Italia

NULLand '\0'are guaranteed to evaluate to 0, so (with appropriate casts) they can be considered identical in value; notice however that they represent two very different things: NULLis a null (always invalid) pointer, while '\0'is the string terminator. EOFinstead is a negative integer constant that indicates the end of a stream; often it's -1, but the standard doesn't say anything about its actual value.

NULL并且'\0'保证评估为 0,因此(使用适当的强制转换)它们可以被认为是相同的值;但是请注意,它们代表两个非常不同的东西:NULL是空(始终无效)指针,而'\0'是字符串终止符。EOF相反是一个负整数常量,表示流的结束;通常它是 -1,但标准没有说明它的实际值。

C & C++ differ in the type of NULLand '\0':

C&C ++中的类型而不同NULL'\0'

  • in C++ '\0'is a char, while in C it's an int; this because in C all character literals are considered ints.
  • in C++ NULLis "just" an integral 0, while in C it can be defined as a 0 casted to void *; this cannot be done in C++ (and it's explicitly forbidden in a note) because, being C++ more strict in pointer conversions, a void *is not implicitly convertible to any other pointer type, so, if NULLwas a void *, it would be necessary to cast it to the target pointer type on assignment:

    int * ptr = (void *) 0; /* valid C, invalid C++ */
    
  • 在 C++ 中'\0'是 a char,而在 C 中它是int; 这是因为在 C 中所有字符文字都被认为是ints。
  • 在 C++NULL中“只是”一个整数 0,而在 C 中它可以定义为一个 0 转换为void *; 这不能在 C++ 中完成(并且在注释中明确禁止),因为 C++ 在指针转换方面更加严格, avoid *不能隐式转换为任何其他指针类型,因此,如果NULL是 a void *,则有必要将其强制转换为赋值时的目标指针类型:

    int * ptr = (void *) 0; /* valid C, invalid C++ */
    

Relevant standard quotations:

相关标准报价:

C++98/03

C++98/03

NULL

NULL

NULLis an integer type guaranteed to evaluate to 0:

NULL是保证计算为 0 的整数类型:

4.10 Pointer conversions

A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4).

18.1 Types

[...] The macro NULL is an implementation-defined C++ null pointer constant in this international Standard (4.10). (Possible definitions include 0and 0L, but not (void*)0).

4.10 指针转换

空指针常量是整数类型的整数常量表达式 (5.19) 右值,其计算结果为零。空指针常量可以转换为指针类型;结果是该类型的空指针值,并且与指向对象的指针或指向函数类型的指针的所有其他值区分开来。相同类型的两个空指针值比较相等。将空指针常量转换为指向 cv 限定类型的指针是一次转换,而不是指针转换后跟限定转换 (4.4) 的序列。

18.1 类型

[...] 宏 NULL 是该国际标准 (4.10) 中实现定义的 C++ 空指针常量。(可能的定义包括00L,但不包括(void*)0)。

'\0'

'\0'

A 0-value char must exist:

必须存在 0 值字符:

2.2 Character sets

The basic execution character set and the basic execution wide-character set shall each contain [...] a null character (respectively, null wide character), whose representation has all zero bits.

2.2 字符集

基本执行字符集和基本执行宽字符集均应包含 [...] 一个空字符(分别为空宽字符),其表示形式全为零。

'\0'is a charliteral:

'\0'char文字:

2.13.2 Character literals

A character literal is one or more characters enclosed in single quotes, as in 'x', optionally preceded by the letter L, as in L'x'. A character literal that does not begin with L is an ordinary character literal, also referred to as a narrow-character literal. An ordinary character literal that contains a single c-charhas type char, with value equal to the numerical value of the encoding of the c-charin the execution character set.

2.13.2 字符字面量

字符文字是用单引号括起来的一个或多个字符,如在 中'x',可选地前面有字母 L,如在 L'x' 中。不以 L 开头的字符文字是普通字符文字,也称为窄字符文字。包含单个c-char的普通字符文字具有 type char,其值等于执行字符集中c-char编码的数值。

and it's value is 0, since that escape sequence specifies its value:

它的值为 0,因为该转义序列指定了它的值:

The escape \oooconsists of the backslash followed by one, two, or three octal digits that are taken to specify the value of the desired character.

转义\ooo符由反斜杠后跟一个、两个或三个八进制数字组成,用于指定所需字符的值。

'\0'is used to terminate strings literals:

'\0'用于终止字符串文字:

2.13.4 String literals

After any necessary concatenation, in translation phase 7 (2.1), '\0'is appended to every string literal so that programs that scan a string can find its end.

2.13.4 字符串字面量

在任何必要的连接之后,在翻译阶段 7 (2.1) 中,'\0'将附加到每个字符串文字,以便扫描字符串的程序可以找到它的结尾。

EOF

EOF

The definition of EOFis delegated to the C89 standard (as stated in §27.8.2 "C Library files"), where it is defined as an implementation specific negative integer.

的定义EOF委托给 C89 标准(如第 27.8.2 节“C 库文件”中所述),其中它被定义为特定于实现的负整数。

C99

C99

NULL

NULL

A null pointer is a 0 integer, optionally casted to void *; NULLis a null pointer.

空指针是一个 0 整数,可选择强制转换为void *; NULL是一个空指针。

6.3.2.3 Pointers

[...] An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. (The macro NULLis defined in <stddef.h>(and other headers) as a null pointer constant; see 7.17.) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

7.17 Common definitions <stddef.h>

[...] The macros are

NULL

which expands to an implementation-defined null pointer constant; [...]

6.3.2.3 指针

[...] 值为 0 的整数常量表达式,或转换为 type 的此类表达式 void *,称为空指针常量。(宏NULL<stddef.h>(和其他头文件)中定义为空指针常量;参见 7.17。)如果将空指针常量转换为指针类型,则结果指针(称为空指针)保证与指针不相等到任何对象或函数。

7.17 通用定义 <stddef.h>

[...] 宏是

NULL

它扩展为实现定义的空指针常量;[...]

'\0'

'\0'

'\0'is an integer with value 0, and is used to terminate strings:

'\0'是一个值为 0 的整数,用于终止字符串:

5.2.1 Character sets

[...] A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string.

6.4.4.4 Character constants

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'. [...]

The octal digits that follow the backslash in an octal escape sequence are taken to be part of the construction of a single character for an integer character constant or of a single wide character for a wide character constant. The numerical value of the octal integer so formed specifies the value of the desired character or wide character. [...]

An integer character constant has type int.

5.2.1 字符集

[...] 所有位都设置为 0 的字节,称为空字符,应存在于基本执行字符集中;它用于终止一个字符串。

6.4.4.4 字符常量

整数字符常量是用单引号括起来的一个或多个多字节字符的序列,如'x'. [...]

八进制转义序列中反斜杠后面的八进制数字被视为整数字符常量的单个字符或宽字符常量的单个宽字符构造的一部分。这样形成的八进制整数的数值指定了所需字符或宽字符的值。[...]

整数字符常量的类型为int

EOF

EOF

EOFis an implementation-defined negative integer

EOF是一个实现定义的负整数

7.19 Input/output <stdio.h>

7.19.1 Introduction

EOF

which expands to an integer constant expression, with type intand a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream

7.19 输入/输出 <stdio.h>

7.19.1 介绍

EOF

它扩展为整数常量表达式,具有类型int和负值,由多个函数返回以指示文件结束,即不再有来自流的输入

回答by Leo Izen

'\0' is always the null character, or 0. EOF depends on the compiler, but is usually -1, and always is a value that an unsigned charcan't hold. Don't rely on the value of EOF being anything, because it CAN CHANGE. Always do x == EOF not x == -1. The value of '\0' is ALWAYS 0. You can count on that.

'\0' 始终为空字符,或 0。EOF 取决于编译器,但通常为 -1,并且始终是 anunsigned char无法容纳的值。不要依赖 EOF 的价值,因为它可以改变。总是做 x == EOF 而不是 x == -1。'\0' 的值始终为 0。您可以指望这一点。

回答by BlackBear

Yes, they are. '\0'has the same value of NULLwhich is 0 (but they mean different things), while EOF is usually -1.

对,他们是。'\0'具有相同的值为NULL0(但它们的含义不同),而 EOF 通常为 -1。

printf("%d",a==EOF); //printed 1

In this case you are asking: is a == EOF? if it is print 1 (which is true), it it's not print 0 (which is false).

在这种情况下,您会问:是 == EOF 吗?如果它是打印 1(这是真的),它不是打印 0(这是假的)。

回答by Kumar Alok

'\0' is always 0

'\0' 总是 0

EOF is compiler dependent

EOF 依赖于编译器

most commonly its -1 (in gcc & g++ it is -1).

最常见的是它的 -1(在 gcc 和 g++ 中它是 -1)。