【问题标题】:The value of EOF different for different specifiers? [duplicate]不同说明符的EOF值不同? [复制]
【发布时间】:2013-08-12 18:02:02
【问题描述】:

我正在使用代码块。

下面是C语言的程序:

/*to find the value of EOF ( End Of the File ) Number */

#include<stdio.h>
int main()
{

    printf("The EOF value is: %d %5ld",EOF, EOF);
    /*when EOF specified in %d specifier it is giving the value as -1, but if its 
         given %ld then a number  4294967295 is printed on the console?*/
    return 0;
}

正如我在节目的 cmets 中所说的:

  • 当在 %d 说明符中指定 EOF 时,它给出的值为 -1,但如果它 给定 %ld 那么控制台上会打印一个数字 4294967295?

我在 HP 上使用 64 位 Linux 操作系统。 [如果编译器为整数、浮点数和长整数分配内存很重要]

【问题讨论】:

    标签: c formatting


    【解决方案1】:

    它可能被定义为(-1),所以它隐含地是int。因此使用%d 打印时正确显示为-1。但是,在预期 long 时将 int 传递给 printf() 会调用未定义的行为。

    可能发生的情况是,您的系统使用 2 的补码,一个 32 位 int 和一个 64 位 long 并且 (int)-1 的位模式与什么相同对应(long)4294967295

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 2020-04-09
      • 2016-04-04
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 2014-12-09
      相关资源
      最近更新 更多