EOF 和 '\0' 的值是多少?在C中

发布于 2024-10-12 10:00:29 字数 308 浏览 7 评论 0原文

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

我打印了两者,EOF 为 -1,'\0' 为 0。但这些值是固定的吗?

我也有这个

int a=-1;

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

Are the value for EOF and '\0'fixed integers?

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

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

I also had this

int a=-1;

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

Are the value for EOF and '\0' fixed integers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

叫嚣ゝ 2024-10-19 10:00:29

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

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

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

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

'\0' is a char with value 0 in C++ and an int with the value 0 in C.

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

情绪 2024-10-19 10:00:29

NULL'\0' 保证计算结果为 0,因此(通过适当的转换)它们可以被认为具有相同的值;但请注意,它们代表两个截然不同的事物:NULL 是 null(始终无效)指针,而 '\0' 是字符串终止符。 EOF 相反是一个负整数常量,表示流的结束;通常它是-1,但标准没有说明它的实际值。

C& C++ 的不同之处在于 NULL'\0' 的类型:

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

    int * ptr = (void *) 0; /* 有效的 C,无效的 C++ */
    

相关标准引用:

C++98/03

NULL

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

4.10 指针转换

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

18.1 类型

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

'\0'

必须存在 0 值字符:

2.2 字符集

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

'\0' 是一个 char 文字:

2.13.2 字符文字

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

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

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

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

2.13.4 字符串文字

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

EOF

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

C99

NULL

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

6.3.2.3 指针

[...] 值为 0 的整数常量表达式,或此类表达式强制转换为类型
void *,称为空指针常量。 (宏NULL(和其他头文件)中定义为空指针常量;参见7.17。)如果空指针常量转换为指针类型,生成的指针(称为空指针)保证与任何对象或函数的指针比较不相等。

7.17 常见定义

[...]宏是

NULL

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

'\0'

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

5.2.1 字符集

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

6.4.4.4 字符常量

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

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

整型字符常量的类型为 int

EOF

EOF 是实现定义的负整数

7.19 输入/输出

7.19.1 简介

EOF

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

NULL and '\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: NULL is a null (always invalid) pointer, while '\0' is the string terminator. EOF instead 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.

C & C++ differ in the type of NULL and '\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++ NULL is "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 NULL was a void *, it would be necessary to cast it to the target pointer type on assignment:

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

Relevant standard quotations:

C++98/03

NULL

NULL is an integer type guaranteed to evaluate to 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 0 and 0L, but not (void*)0).

'\0'

A 0-value char must exist:

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.

'\0' is a char literal:

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-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set.

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

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

'\0' is used to terminate strings literals:

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.

EOF

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

C99

NULL

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

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 NULL is 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; [...]

'\0'

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

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.

EOF

EOF is an implementation-defined negative integer

7.19 Input/output <stdio.h>

7.19.1 Introduction

EOF

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

哎呦我呸! 2024-10-19 10:00:29

'\0' 始终为空字符或 0。EOF 取决于编译器,但通常为 -1,并且始终是 unsigned char 无法保存的值。不要依赖 EOF 的值,因为它可以改变。始终执行 x == EOF 而不是 x == -1。 '\0' 的值始终为 0。您可以信赖这一点。

'\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 char can'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.

苏别ゝ 2024-10-19 10:00:29

是的,他们是。 '\0'NULL 的值相同,均为 0(但含义不同),而 EOF 通常为 -1。

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

在这种情况下,您会问:是 == EOF 吗?如果是 print 1(为真),则不是 print 0(为假)。

Yes, they are. '\0' has the same value of NULL which is 0 (but they mean different things), while EOF is usually -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).

丑丑阿 2024-10-19 10:00:29

'\0' 始终为 0

EOF 取决于编译器,

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

'\0' is always 0

EOF is compiler dependent

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文