printf: Ambiguous warning

Jakub Jelinek jakub@redhat.com
Tue Mar 16 10:26:29 GMT 2021


On Tue, Mar 16, 2021 at 11:20:05AM +0100, Rene Kita wrote:
> (Please keep me CC'd, I'm not subscribe to the list)
> 
> Here is a minimal example:
> #include <stdio.h>
> 
> int
> main()
> {
>   unsigned short n;
>   unsigned short *p;
>   p = &n;
> 
>   printf("p: %hn\n", p);
> }
> 
> 
> % gcc -Wall -Wpedantic main.c
> main.c: In function 'main':
> main.c:10:16: warning: format '%hn' expects argument of type 'short int *', but argument 2 has type 'short unsigned int *' [-Wformat=]
>    10 |   printf("p: %hn\n", p);
>       |              ~~^     ~
>       |                |     |
>       |                |     short unsigned int *
>       |                short int *
>       |              %hn
> 
> The warning for line 10 suggests to use '%hn' as format specifier which
> is already used and the wrong one. AFAIK the correct format specifier
> would be '%p' here.

No, the warning tells you that argument for %hn should have short int *
type, not unsigned short int *.
C99 says for n:
"The argument shall be a pointer to signed integer into which is written the
number of characters written to the output stream so far by this call to
fprintf. No argument is converted, but one is consumed. If the conversion
specification includes any flags, a field width, or a precision, the behavior is
undefined."
Note the SIGNED in there.  And for the h modifier:
"or that a following n conversion specifier applies to a pointer to a short
int argument."
(not unsigned short int).

> I didn't not find a bug report for this. Is this a known problem? Should
> I file a bug report for this?

No, this is a bug in your testcase that is correctly diagnosed.

	Jakub



More information about the Gcc mailing list