EOF 和 '\0' 的值是多少?在C中
我知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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 typeint
and an implementation dependent negative value but is very commonly -1.'\0'
is achar
with value 0 in C++ and anint
with the value 0 in C.The reason why
printf("%d",a==EOF);
resulted in1
was because you didn't assign the valueEOF
toa
. Instead you checked ifa
was equal toEOF
and since that was true (a == -1 == EOF
) it printed1
.NULL
和'\0'
保证计算结果为 0,因此(通过适当的转换)它们可以被认为具有相同的值;但请注意,它们代表两个截然不同的事物:NULL
是 null(始终无效)指针,而'\0'
是字符串终止符。EOF
相反是一个负整数常量,表示流的结束;通常它是-1,但标准没有说明它的实际值。C& C++ 的不同之处在于
NULL
和'\0'
的类型:'\0'
是char
,而在 C 中它是一个int
;这是因为在 C 中,所有字符文字都被视为 int。在 C++ 中
NULL
“只是”一个整数 0,而在 C 中它可以定义为 0 转换为void *
;这不能在 C++ 中完成(并且在注释中明确禁止),因为 C++ 在指针转换方面更加严格,void *
不能隐式转换为任何其他指针类型,因此,如果NULL
是一个void *
,有必要在赋值时将其转换为目标指针类型:相关标准引用:
C++98/03
NULL
NULL
是保证计算结果为 0 的整数类型:'\0'
必须存在 0 值字符:
'\0'
是一个char
文字:它的值为 0,因为转义序列指定了它的值:
'\0'
用于终止字符串文字:EOF
EOF
的定义委托给 C89 标准(如第 §27.8.2“C 库文件”中所述),其中它被定义为特定于实现的负整数。C99
NULL
空指针是一个 0 整数,可以选择转换为
void *
;NULL
是一个空指针。'\0'
'\0'
是一个值为 0 的整数,用于终止字符串:EOF
EOF
是实现定义的负整数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'
:'\0'
is achar
, while in C it's anint
; this because in C all character literals are consideredint
s.in C++
NULL
is "just" an integral 0, while in C it can be defined as a 0 casted tovoid *
; this cannot be done in C++ (and it's explicitly forbidden in a note) because, being C++ more strict in pointer conversions, avoid *
is not implicitly convertible to any other pointer type, so, ifNULL
was avoid *
, it would be necessary to cast it to the target pointer type on assignment:Relevant standard quotations:
C++98/03
NULL
NULL
is an integer type guaranteed to evaluate to 0:'\0'
A 0-value char must exist:
'\0'
is achar
literal:and it's value is 0, since that escape sequence specifies its value:
'\0'
is used to terminate strings literals: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.'\0'
'\0'
is an integer with value 0, and is used to terminate strings:EOF
EOF
is an implementation-defined negative integer'\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.是的,他们是。
'\0'
与NULL
的值相同,均为 0(但含义不同),而 EOF 通常为 -1。在这种情况下,您会问:是 == EOF 吗?如果是 print 1(为真),则不是 print 0(为假)。
Yes, they are.
'\0'
has the same value ofNULL
which is 0 (but they mean different things), while EOF is usually -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).
'\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).