-Werror=format= warnings about identical types

lotharyx
Posts: 12
Joined: Fri Dec 13, 2019 4:41 am

-Werror=format= warnings about identical types

Postby lotharyx » Wed Apr 30, 2025 3:40 pm

I'm not sure if this is an ESP-IDF thing or a GCC thing, but I sometimes get printf format warnings about congruent types. Most commonly it's long unsigned int vs unsigned int, which are the same size, proven at runtime:

Code: Select all

    ESP_LOGI("main", "sizeof(unsigned int) = %d", sizeof(unsigned int));
    ESP_LOGI("main", "sizeof(unsigned long) = %d", sizeof(unsigned long));
    ESP_LOGI("main", "sizeof(long unsigned int) = %d", sizeof(long unsigned int));
    ESP_LOGI("main", "sizeof(uint32_t) = %d", sizeof(uint32_t));
Output:

Code: Select all

I (399) main: sizeof(unsigned int) = 4
I (399) main: sizeof(unsigned long) = 4
I (399) main: sizeof(long unsigned int) = 4
I (399) main: sizeof(uint32_t) = 4
I see this most often when using %u for a uint32_t:

Code: Select all

esp_log_color.h:100:31: error: format '%u' expects argument of type 'unsigned int', but argument 7 has type 'long unsigned int' [-Werror=format=]
It seems to me that the compiler should be smart enough to realize that two unsigned 4-byte integer types are interchangeable. Why doesn't it?

MicroController
Posts: 2694
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: -Werror=format= warnings about identical types

Postby MicroController » Wed Apr 30, 2025 9:56 pm

Different types are different types to gcc in this case. Note that you may compile the same C code for a different platform/architecture (e.g. 64-bit PC) with the same types having different sizes and/or layouts. I think int and long int being the same size is rather an exception than the norm across various platforms, so gcc tells you that your code may be erroneous and is at least not portable.

Who is online

Users browsing this forum: Amazon [Bot], PetalBot and 3 guests