Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   is this compiler diagnostic legal? (http://www.velocityreviews.com/forums/t590918-is-this-compiler-diagnostic-legal.html)

Rainer Zufall 02-11-2008 01:38 PM

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;


Martin 02-11-2008 01:47 PM

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

Walter Roberson 02-11-2008 04:07 PM

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

Robert Gamble 02-11-2008 04:26 PM

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

Martin 02-11-2008 04:51 PM

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

Keith Thompson 02-11-2008 05:08 PM

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"

Hans Schneider 02-11-2008 11:16 PM

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?


Keith Thompson 02-12-2008 12:06 AM

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"

jacob navia 02-12-2008 12:09 AM

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

Peter Nilsson 02-12-2008 01:33 AM

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 06:07 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.