![]() |
is this compiler diagnostic legal?
consider this code #include <stdio.h> int main(void) { unsigned long ul = 42; printf("0x%08lX\n", ul); return 0; } One compiler I tried issues the following diagnostic: Warning foo.c: 5 printf argument mismatch for format X. Expected long int got unsigned long 0 errors, 1 warning I always thought %X requires an unsigned quantity. Who is right here, my understanding of the standard, or the compiler? -- Rainer Zufall; |
Re: is this compiler diagnostic legal?
On Feb 11, 1:38*pm, Rainer Zufall <rainer.zuf...@gmx.ch.invalid>
wrote: > I always thought %X requires an unsigned quantity. %lX converts a long argument into an unsigned long then to an unsigned sequence of (in the case of %08lX) at least 8 hexadecimal digits, padded with leading zeros if necessary. The compiler is correct. -- Martin |
Re: is this compiler diagnostic legal?
In article <0c84b548-558c-4a61-9afa-9bd169981a87@u10g2000prn.googlegroups.com>,
Martin <martin.o_brien@which.net> wrote: >On Feb 11, 1:38=A0pm, Rainer Zufall <rainer.zuf...@gmx.ch.invalid> >wrote: >> I always thought %X requires an unsigned quantity. >%lX converts a long argument into an unsigned long then to an unsigned >sequence of (in the case of %08lX) at least 8 hexadecimal digits, >padded with leading zeros if necessary. C89 4.9.6.1 The fprintf Function o,u,x,X The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal notation (x or X) [...] -- "No one has the right to destroy another person's belief by demanding empirical evidence." -- Ann Landers |
Re: is this compiler diagnostic legal?
On Feb 11, 8:38 am, Rainer Zufall <rainer.zuf...@gmx.ch.invalid>
wrote: > consider this code > > #include <stdio.h> > int main(void) > { > unsigned long ul = 42; > printf("0x%08lX\n", ul); > return 0; > } > > One compiler I tried issues the following diagnostic: > > Warning foo.c: 5 printf argument mismatch for format X. Expected long int got unsigned long > 0 errors, 1 warning > > I always thought %X requires an unsigned quantity. > > Who is right here, my understanding of the standard, or the compiler? %X does always require an unsigned type, %lX requires a type of unsigned long which you properly provided. You are correct, your compiler is wrong. -- Robert Gamble |
Re: is this compiler diagnostic legal?
On Feb 11, 1:38*pm, Rainer Zufall <rainer.zuf...@gmx.ch.invalid>
wrote: > I always thought %X requires an unsigned quantity. > > Who is right here, my understanding of the standard, or the compiler? I've looked into this after seeing the previous replies. Plauger in "The Standard C Library" states that the four conversion specifiers lo, lX, lx, and lu expect long arguments, which are then converted to unsigned long. K&R2 states that o, x, X, u expect the argument type to be int (i.e., signed) - although the errata for that book corrects that to unsigned int. That implies lx expects the argument type to be unsigned long. However, the most important resource, the C Standard, specifies clearly that unsigned long is indeed the expected argument type for the conversion specifiers, including the one you queried. The compiler has got it wrong. I shall annotate Plauger accordingly. -- Martin |
Re: is this compiler diagnostic legal?
Martin <martin.o_brien@which.net> writes:
> On Feb 11, 1:38Â*pm, Rainer Zufall <rainer.zuf...@gmx.ch.invalid> > wrote: >> I always thought %X requires an unsigned quantity. > > %lX converts a long argument into an unsigned long then to an unsigned > sequence of (in the case of %08lX) at least 8 hexadecimal digits, > padded with leading zeros if necessary. > > The compiler is correct. For context, the code was: #include <stdio.h> int main(void) { unsigned long ul = 42; printf("0x%08lX\n", ul); return 0; } and the warning was: Warning foo.c: 5 printf argument mismatch for format X. Expected long int got unsigned long 0 errors, 1 warning The format requires an unsigned long argument. The compiler's warning is incorrect. This isn't actually a violation of the standard; the standard allows additional diagnostics, and doesn't require them to be factually correct. But it's certainly a bug. If the actual argument were of type signed long, a compiler could reasonably refrain from warning about it, but it cannot reasonably warn about this call, which is perfectly correct. -- Keith Thompson (The_Other_Keith) <kst-u@mib.org> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
is this compiler diagnostic legal?
Am Mon, 11 Feb 2008 14:38:16 +0100 (CET), Rainer Zufall <rainer.zufall@gmx.ch.invalid> schrieb:
> >consider this code > > #include <stdio.h> > int main(void) > { > unsigned long ul = 42; > printf("0x%08lX\n", ul); > return 0; > } > >One compiler I tried issues the following diagnostic: > > Warning foo.c: 5 printf argument mismatch for format X. Expected long int got unsigned long > 0 errors, 1 warning > >I always thought %X requires an unsigned quantity. > >Who is right here, my understanding of the standard, or the compiler? LCC-WIN32 shows this warning also, but I think it has more sense. unsigned long is always positive. signed long can positive or negative be. What if I want to print negative numbers in HeX? |
Re: is this compiler diagnostic legal?
Hans Schneider <hans@localhost.localdomain> writes:
> Am Mon, 11 Feb 2008 14:38:16 +0100 (CET), Rainer Zufall > <rainer.zufall@gmx.ch.invalid> schrieb: >> >>consider this code >> >> #include <stdio.h> >> int main(void) >> { >> unsigned long ul = 42; >> printf("0x%08lX\n", ul); >> return 0; >> } >> >>One compiler I tried issues the following diagnostic: >> >> Warning foo.c: 5 printf argument mismatch for format X. Expected long int got unsigned long >> 0 errors, 1 warning >> >>I always thought %X requires an unsigned quantity. >> >>Who is right here, my understanding of the standard, or the compiler? > > LCC-WIN32 shows this warning also, but I think it has more sense. Since there's nothing wrong with the code, I hope lcc-win32 doesn't show a warning for it. > unsigned long is always positive. > > signed long can positive or negative be. > > What if I want to print negative numbers in HeX? There's no direct way to do that using printf. If x is your number, you could do something like this: printf("x = %s0x%x\n", x < 0 ? "-" : "", (unsigned)abs(x)); but it's not guaranteed to work for x == INT_MIN. -- Keith Thompson (The_Other_Keith) <kst-u@mib.org> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
Re: is this compiler diagnostic legal?
Hans Schneider wrote:
> LCC-WIN32 shows this warning also, but I think it has more sense. > Not in the latest version. We had a discussion about this some months ago and I fixed it, as far as I can remember. In any case I see absolutely no warning with the latest version of lcc-win -- jacob navia jacob at jacob point remcomp point fr logiciels/informatique http://www.cs.virginia.edu/~lcc-win32 |
Re: is this compiler diagnostic legal?
Robert Gamble <rgambl...@gmail.com> wrote:
> %X does always require an unsigned type, %lX requires a > type of unsigned long... %X requires unsigned int, but observe that %hX requires the promoted type of unsigned short, which may be int. -- Peter |
| All times are GMT. The time now is 11:21 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.