NO

Author Topic: printf format specifier warning always says the signed type  (Read 433 times)

Offline FRex

  • Member
  • *
  • Posts: 4
Here's an example program:
Code: [Select]
#include <stdio.h>

int main(void)
{
    long long a = 10;
    printf("%u\n", a);
    return 0;
}

It produces this warning: main.c(6): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'long long int'.

It's similar for llu, u, x, etc. any specifier that deals with unsigned types.

It will produce a warning if a type of wrong size is used (so using int with x or u, or unsigned with d is fine even with no explicit cast to right type) but will say that it expected the signed type of that width (so when you pass something of other size to u, it will say int in the warning, not unsigned int).

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1881
Re: printf format specifier warning always says the signed type
« Reply #1 on: May 28, 2019, 08:39:18 am »
Not a bug
http://www.cplusplus.com/reference/cstdio/printf/
Code: [Select]
#include <stdio.h>

int main(void)
{
    long long a = 10;
    printf("%lld\n", a);
    return 0;
}
use correct prefix.
« Last Edit: May 28, 2019, 04:41:53 pm by TimoVJL »
May the source be with you

Offline FRex

  • Member
  • *
  • Posts: 4
Re: printf format specifier warning always says the signed type
« Reply #2 on: May 28, 2019, 01:22:13 pm »
%u is for unsigned. If you use wrong type with it it will say it expected an int. This is the bug - the warning says the wrong type - the signed one.

GCC:  warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'long long int' [-Wformat=]
MSVC: warning C4477: 'printf' : format string '%u' requires an argument of type 'unsigned int', but variadic argument 1 has type '__int64'
Pelles: warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'long long int'.

I have changed a type in a struct in my program from unsigned to unsigned long long and I expected to have to fix a bunch of "expected unsigned in printf" kind of errors, so getting a bunch of "expected int" has threw me off because I know I'm careful with my format specifiers and didn't use %d for printing that one variable so why do all the warnings mention int.
« Last Edit: May 28, 2019, 01:34:14 pm by FRex »