64位环境中的printf / snprintf行为

时间:2019-04-23 05:54:00

标签: c printf 64-bit

0的64位环境中打印%lu得到140733193388032的输出。

我在64位环境中打印了8次0,直到第6位的值都打印为0,最后2次正在打印140733193388032

#include <stdio.h>

struct size {
    unsigned long length;
    unsigned long breadth;
};

struct pad {
    unsigned long len;
    unsigned long bre;
    unsigned char hei;
    unsigned char pad[7];
};

int main() {
    unsigned long a;
    unsigned long b = 0;
    struct size sz;
    struct pad sz1;

    printf("%lu is a, %lu is b , %lu is length, %lu is breadth, %lu is len, "
           "%lu is bre, %lu is hei, %lu is zero\n",
           a, b, sz.length, sz.breadth, sz1.len, sz1.bre, sz1.hei, 0);
    printf("%lu is a, %lu is b , %lu is length, %lu is breadth, %lu is len, "
           "%lu is bre, %lu is hei, %lu is zero",
           0, 0, 0, 0, 0, 0, 0, 0);
}

输出:

140737488347824 is a, 0 is b , 0 is length, 4195408 is breadth, 140737346312864 is len, 4195893 is bre, 140733193388064 is hei, 140733193388032 is zero
0 is a, 0 is b , 0 is length, 0 is breadth, 0 is len, 0 is bre, 140733193388032 is hei, 140733193388032 is zero

通过初始化结构体和局部:

#include <stdio.h>

struct size {
    unsigned long length;
    unsigned long breadth;
};

struct pad {
    unsigned long len;
    unsigned long bre;
    unsigned char hei;
    unsigned char pad[7];
};

int main() {
    unsigned long a = 0;
    unsigned long b = 0;
    struct size sz = { 0 };
    struct pad sz1 = { 0 };

    printf("%lu is a, %lu is b , %lu is length, %lu is breadth, %lu is len, "
           "%lu is bre, %lu is hei, %lu is zero\n",
           a, b, sz.length, sz.breadth, sz1.len, sz1.bre, sz1.hei, 0);
    printf("%lu is a, %lu is b , %lu is length, %lu is breadth, %lu is len, "
           "%lu is bre, %lu is hei, %lu is zero",
           0, 0, 0, 0, 0, 0, 0, 0);
}

输出:

0 is a, 0 is b , 0 is length, 0 is breadth, 0 is len, 0 is bre, 140733193388032 is hei, 140733193388032 is zero
0 is a, 0 is b , 0 is length, 0 is breadth, 0 is len, 0 is bre, 140733193388032 is hei, 140733193388032 is zero

3 个答案:

答案 0 :(得分:1)

您所引用的变量未初始化,并且包含内存中的一些垃圾。编译器没有必要进行零初始化,因此数字是零。在声明时将所有变量初始化为零,如下所示:

memset(&sz, 0, sizeof(sz))

或单独分配所有成员。

提供摘录后进行编辑

我试图对所有变量进行零初始化:

#include <stdio.h>
struct size
{
  unsigned long length;
  unsigned long breadth;
};

struct pad
{
  unsigned long len;
  unsigned long bre;
  unsigned char hei;
  unsigned char pad[7];
};


int main()
{

  unsigned long a = 0;
  unsigned long b = 0;
  struct size sz = { 0 };
  struct pad sz1 = { 0 };

  printf("%lu is a, %lu is b , %lu is length, %lu is breadth, %lu is len, %lu is bre, %lu is hei, %lu is zero\n",a,b,sz.length, sz.breadth, sz1.len, sz1.bre, sz1.hei, 0);
  printf("%lu is a, %lu is b , %lu is length, %lu is breadth, %lu is len, %lu is bre, %lu is hei, %lu is zero",0,0,0,0,0,0, 0, 0);
 }

我得到以下输出:

0 is a, 0 is b , 0 is length, 0 is breadth, 0 is len, 0 is bre, 0 is hei, 0 is zero
0 is a, 0 is b , 0 is length, 0 is breadth, 0 is len, 0 is bre, 0 is hei, 0 is zero

答案 1 :(得分:0)

#include <stdio.h>

struct size
{

  unsigned long length;
  unsigned long breadth;
};

struct pad
{
  unsigned long len;
  unsigned long bre;
  unsigned char hei;
  unsigned char pad[7];
};


int main()
{

   unsigned long a = 0;
   unsigned long b = 0;
   struct size sz = {0};
   struct pad sz1= {0};

   printf("%lu is a, %lu is b , %lu is length, %lu is breadth, %lu is len, %lu is 
   bre, %uc is hei, %u is zero\n",a,b,sz.length, sz.breadth, sz1.len, sz1.bre, 
   sz1.hei, 0);
   printf("%u is a, %u is b , %u is length, %u is breadth, %u is len, %u is bre, %u 
   is hei, %u is zero",0,0,0,0,0,0, 0, 0);
  }

此输出全为0

在gcc中使用-wformat,修复了警告,然后打印了全0的

答案 2 :(得分:0)

您的代码中有2个问题:

  • 您正在打印未初始化的数据,行为是不确定的,输出是不可预测的。
  • sz1.hei的类型为unsigned char,您不应使用格式%lu,或将此值强制转换为(unsigned long)sz1.hei
  • 0的类型为int,您应在第二个0L中将0UL的格式传递为%luprintf

对于初始化程序,此代码在32位(和64位Windows)中的行为与预期相同,因为unsigned long在您的平台上是32位的,大小与unsigned int相同。

对于每个转换说明符,您都必须传递带有预期类型的​​printf参数,否则该行为是不确定的。您应该提高警告级别(例如gcc -Wall -Wextra -Werror),以使编译器警告您此类错误以及许多其他错误。

这是更正的版本:

#include <stdio.h>

struct size {
    unsigned long length;
    unsigned long breadth;
};

struct pad {
    unsigned long len;
    unsigned long bre;
    unsigned char hei;
    unsigned char pad[7];
};

int main() {
    unsigned long a = 0;
    unsigned long b = 0;
    struct size sz = { 0 };
    struct pad sz1 = { 0 };

    printf("%lu is a, %lu is b , %lu is length, %lu is breadth, %lu is len, "
           "%lu is bre, %lu is hei, %lu is zero\n",
           a, b, sz.length, sz.breadth, sz1.len, sz1.bre, (unsigned long)sz1.hei, 0UL);
    printf("%lu is a, %lu is b , %lu is length, %lu is breadth, %lu is len, "
           "%lu is bre, %lu is hei, %lu is zero",
           0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL);
    return 0;
}