C99 standard and gnu libc printf



C99标准(ISO/IEC 9899:1999(说,PINTF格式占位符的长度字段可以是 l ,仅适用于float号码。在printf(3(manpage中也证实了这一点:

l

a以下 a a e e f f g g 转换对应于A long double double 参数。

如果我编译了此:

printf("%Ld",3);

使用 -STD = C99 GCC参数,我得到警告:

warning: format ‘%Ld’ expects argument of type ‘long long int’, but argument 2 has type ‘int’ [-Wformat=]
  printf("%Ld",3);
           ^

GNU LIBC似乎没有folloe C99标准

我有GCC 6.3.0,GNU LIBC 2.24,我的主机是Linux Ubuntu 17.04 64bit

[编辑]

如果我编译了此:

printf("%Ldn",3);
printf("%lldn", 3LL);
printf("%Lfn", 3.3L);
printf("%llfn", 3.3L);
printf("%fn", 3.3L);

我得到:

warning: format ‘%Ld’ expects argument of type ‘long long int’, but argument 2 has type ‘int’ [-Wformat=]
  printf("%Ldn",3);
            ^
warning: use of ‘ll’ length modifier with ‘f’ type character has either no effect or undefined behavior [-Wformat=]
  printf("%llfn", 3.3L);
             ^
warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘long double’ [-Wformat=]
  printf("%fn", 3.3L);
           ^

因此,预期的警告消息与%llf一样。

使用-pedinantic我收到更多的详细消息:

warning: ISO C does not support the ‘%Ld’ gnu_printf format [-Wformat=]
  printf("%Ldn",3);
            ^
 warning: format ‘%Ld’ expects argument of type ‘long long int’, but argument 2 has type ‘int’ [-Wformat=]
 warning: use of ‘ll’ length modifier with ‘f’ type character has either no effect or undefined behavior [-Wformat=]
   printf("%llfn", 3.3L);
              ^
 warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘long double’ [-Wformat=]
   printf("%fn", 3.3L);
            ^

[编辑]

我有GCC ML

的邮件

看来允许"%ld"并需要 "长long int"。

,但GNU libc允许,这就是重点。对于GNU LIBC%LD 含义与%lld相同,但这是非标准的,所以当您使用时 - Pesterantic您会收到警告。

此解释警告消息。

标准说:

如果长度修饰符出现在上面指定以外的任何转换说明符中出现,则行为是未定义的。

在这种情况下, L长度修饰符未指定用于d转换符,因此该行为是未定义的。

大概是GCC(这是GCC给您警告,而不是LIBC(正在使用相同的内部标志来进行长时间的双打和长时间的长时间。如果警告更精确,那将是很好的,但是根本不需要给您任何警告(或特别执行其他警告(,因此它正确遵循标准。

最新更新