关于格式化的 Sprintf 警告

Sprintf warnings about formatting

我有一个关于 sprintf c 格式的问题。无论我用哪种方式格式化数据,编译器都会发现另一个问题。对于初学者,请考虑以下代码:

#include <stdio.h>
#include <stdint.h>

int main(void) 
{
    char text[100];
    uint8_t hours = 1, minutes = 10, seconds = 20;
    int32_t milisec = 300;

    sprintf(text, "[%02u:%02u:%02u.%03i]", hours, minutes, seconds, milisec);
    printf("%s", text);

    return 0;
}

编译器抱怨:

warning: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t {aka long unsigned int}' [-Wformat=]
warning: format '%u' expects argument of type 'unsigned int', but argument 4 has type 'uint32_t {aka long unsigned int}' [-Wformat=]
warning: format '%u' expects argument of type 'unsigned int', but argument 5 has type 'uint32_t {aka long unsigned int}' [-Wformat=]
warning: format '%i' expects argument of type 'int', but argument 6 has type 'int32_t {aka long int}' [-Wformat=]

如果我将 %u 更改为 %lu 我得到:

warning: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'int' [-Wformat=]

header <inttypes.h> 提供了 PRIu8PRIi32 等宏,它们是标准整数类型 uint8_tint32_t。优点是这些宏是可移植的并且包含正确的格式说明符,即使使用不同的基本类型来实现这些整数类型也是如此。

您需要为这些类型使用适当的 formatting macro。例如

#include <inttypes.h>

uint32_t hours = 1, minutes = 10, seconds = 20;
int32_t milisec = 300;

sprintf(text, "[%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%03" PRId32 "]",
        hours, minutes, seconds, milisec);

在上面的片段中,PRIu32 用于打印十进制的 uint32_tPRId32 用于打印 int32_t

另一种选择是对所有这些值只使用 int/unsigned,并希望 32767/65535 小时对每个人都足够。