Printing an unsigned char

  • Thread starter Enrico `Trippo' Porreca
  • Start date
E

Enrico `Trippo' Porreca

Suppose I wanted to print an unsigned char (in hex). Should I say:

unsigned char x = 0x12;

printf("%X\n", x);

or

printf("%X\n", (unsigned) x);

?

If understand it correctly, x is converted to int (if int has a suitable
range of values), because I'm passing it to a variadic function.

I read different words in my C89 and C99 drafts: in the former, "%X"
wants an int, while in the latter it needs an unsigned int.

So the is cast needed in C99? If so, will the program be illegal in C89?
 
R

Robert W Hand

I read different words in my C89 and C99 drafts: in the former, "%X"
wants an int, while in the latter it needs an unsigned int.

My copy of C89 states on page 133 that "%X" takes an unsigned int
argument. The C99 Standard makes the same claim. Where did you see
that "%X" takes an int in C89?

Best wishes,

Bob
 
E

Enrico `Trippo' Porreca

Robert said:
My copy of C89 states on page 133 that "%X" takes an unsigned int
argument. The C99 Standard makes the same claim. Where did you see
that "%X" takes an int in C89?

In the C89 draft taken from http://cern.ch/dan.pop/ansi.c I read:

The conversion specifiers and their meanings are

d, i, o, u, x, X The int argument is converted to signed decimal ( d
or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
hexadecimal notation ( x or X ); the letters abcdef are used for x
conversion and the letters ABCDEF for X conversion.

Maybe it was changed in the actual standard?
 
?

=?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=

In the C89 draft taken from http://cern.ch/dan.pop/ansi.c I read:

The conversion specifiers and their meanings are

d, i, o, u, x, X The int argument is converted to signed decimal ( d
or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
hexadecimal notation ( x or X ); the letters abcdef are used for x
conversion and the letters ABCDEF for X conversion.

Maybe it was changed in the actual standard?
Point on. "or unsigned hexadecimal notation ( x or X )".
Means x and X are unsigned int.
 
J

Joe Wright

Nils said:
Point on. "or unsigned hexadecimal notation ( x or X )".
Means x and X are unsigned int.

Reading impaired? It says the int argument corresponding to the 'x'
or 'X' conversion specifiers is converted to unsigned.

Regard..

#include <stdio.h>

int main(void) {
int x = -2;
printf("%#x\n", x);
return 0;
}

...prints 0xfffffffe
 
D

Dan Pop

In said:
In the C89 draft taken from http://cern.ch/dan.pop/ansi.c I read:

The conversion specifiers and their meanings are

d, i, o, u, x, X The int argument is converted to signed decimal ( d
or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
hexadecimal notation ( x or X ); the letters abcdef are used for x
conversion and the letters ABCDEF for X conversion.

Maybe it was changed in the actual standard?

Yes, this is one of the changes between the last public draft and the
final C89 standard (K&R2 still needs to be fixed). The other I can
remember right now is CLK_TCK being replaced by CLOCKS_PER_SEC.

Dan
 
D

Dan Pop

In said:
Suppose I wanted to print an unsigned char (in hex). Should I say:

unsigned char x = 0x12;

printf("%X\n", x);

or

printf("%X\n", (unsigned) x);

?

If understand it correctly, x is converted to int (if int has a suitable
range of values), because I'm passing it to a variadic function.

I read different words in my C89 and C99 drafts: in the former, "%X"
wants an int, while in the latter it needs an unsigned int.

The final C89 standard has the same wording as the C99 draft: %X expects
unsigned int.
So the is cast needed in C99?

Not really, because in C99 you can use %hhX and leave x uncast.
If so, will the program be illegal in C89?

The one without the cast, yes, if the standard is read at its strictest.
The committee intent, as expressed in a non-normative footnote was to
allow it, as long as the value of x is in the range of both types.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top