c - 警告 :0 flag ignored with precision and '%x' gnu_printf format

标签 c data-structures warnings printf

我在用 C 编译时收到以下警告。

   ../tcpuip/uip_arp.c: In function 'display_arp_table':
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_p
    rintf format
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_p
    rintf format
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_p
    rintf format
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_p
    rintf format
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_p
    rintf format
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_p
    rintf format
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_p
    rintf format
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_p
    rintf format
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_p
    rintf format
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_p
    rintf format
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_p
    rintf format
    ../tcpuip/uip_arp.c:547: warning: '0' flag ignored with precision and '%x' gnu_printf format

错误来的那一行是

        RELEASE_MSG("MAC: %0.2x.%0.2x.%0.2x.%0.2x.%0.2x.%0.2x  ",(unsigned char)tabptr->ethaddr.addr[0],(unsigned char)tabptr->ethaddr.addr[1],(unsigned char)tabptr->ethaddr.addr[2],(unsigned char)tabptr->ethaddr.addr[3],(unsigned char)tabptr->ethaddr.addr[4],(unsigned char)tabptr->ethaddr.addr[5]);

tabptr 是struct arp_entry 的指针

PACKED struct arp_entry {
  u16_t ipaddr[2];
  struct uip_eth_addr ethaddr;
  u8_t time;
#ifdef _ALIGNED_
  u8_t dummy;
#endif
}

ethadder 是 struct uip_eth_addr 的指针

PACKED struct uip_eth_addr {
  u8_t addr[6];
}  ;

如果有人能分享一些关于这个警告的信息,请告诉我。我只知道 %0.2x 表示字符为两位十六进制。帮助!

最佳答案

格式字符串%0.2x表示:

  • 打印一个无符号的十六进制整数
  • 至少打印两个数字
  • 从左边用零填充

由于精度定义了要输出的最小位数,因此将忽略零填充(根据规范)。您可以使用 %.2x%02x 来执行您想要的操作。

得到

   1
  11
55555

您将使用 %4x

  01
  11
 111

将是 %4.2x

0001
0011
55555

将是 %04x%.4x

请注意,值有符号时是有区别的。精度定义了位数 的数量,这意味着该值将多占用一个符号空间。对于最小输出宽度,符号已经计算在内:

$ printf "%.5i\n%05i\n" '-11' -11
-00011
-0011

关于c - 警告 :0 flag ignored with precision and '%x' gnu_printf format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10071924/

相关文章:

java - 字符串字谜的边界条件

c - 查看 AVL 树未给出正确的输出

c++ - 警告信息的目的?

Java 警告 : [options] system modules path not set in conjunction with -source 11

c - 来自固定大小缓冲区的 malloc 实现

c - 如何将一个节点从一个链表添加到另一个链表

c - 使用 getch() 和 getchar() 清除输入缓冲区的区别?

c - 如何仅针对特定行将文本文件中的单词读入数组?

algorithm - 加快搜索最佳二进制匹配数

perl:为什么 $hashsize = keys $hash{$foo} 会给出实验警告,我该如何写得更好?