Compiling this MCVE
#include <stdio.h>
#include <climits>
#include <limits>
int main()
{
unsigned long x = ULONG_MAX;
printf( "1:=%lu - 2:=%lu 3:=%ld\n", std::numeric_limits<size_t>::max(), x, x );
}
with these gcc/clang [any version] options -Wall -Werror -pedantic
does not fail.
This is the output:
1:=18446744073709551615 - 2:=18446744073709551615 3:=-1
I expected an error because I provide an unsigned int
but the format specifier is signed int
.
Compiling it with PowerPC gcc 4.8 fails as expected:
error: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'unsigned int' [-Werror=format=]
My question is:
Why do gcc/clang compile these format specifier? For me I would say PowerPC gcc is correct because this is a serious sign/unsigned issue and the wrong format specifier displays an incorrect result.