473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Printing an unsigned char

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?

--
Enrico `Trippo' Porreca

Nov 14 '05 #1
6 13793
On Fri, 04 Jun 2004 18:27:49 +0200, Enrico `Trippo' Porreca
<ep******@people.it> wrote:
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
Nov 14 '05 #2
Robert W Hand wrote:
On Fri, 04 Jun 2004 18:27:49 +0200, Enrico `Trippo' Porreca
<ep******@people.it> wrote:
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?


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?

--
Enrico `Trippo' Porreca

Nov 14 '05 #3
On Fri, 04 Jun 2004 23:27:29 +0200, Enrico `Trippo' Porreca wrote:

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.
Nov 14 '05 #4
Nils O. Selåsdal wrote:
On Fri, 04 Jun 2004 23:27:29 +0200, Enrico `Trippo' Porreca wrote:
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.


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

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #5
In <40**************@people.it> Enrico `Trippo' Porreca <ep******@people.it> writes:
Robert W Hand wrote:
On Fri, 04 Jun 2004 18:27:49 +0200, Enrico `Trippo' Porreca
<ep******@people.it> wrote:
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?


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
In <40**************@people.it> Enrico `Trippo' Porreca <ep******@people.it> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Steven T. Hatton | last post by:
I'm trying to write a program like hexel. I guess I could fish out the source for hexel and look at that, but for now I'm trying to figure out how I can do with with std::stringstream and...
7
by: crazy | last post by:
Hi It seems something really simple, but can't figure out. It's not my homework :), was just looking at some C problems on the web and came across this one. why doesn't the following code print...
10
by: ben | last post by:
i'm learning about the floating point format that's used on my computer (apple mac so powerpc) i've written a function to print out the bits in a float to see how floats are represented and i...
42
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is...
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
14
by: abhishekkarnik | last post by:
Hi, I am trying to read an exe file and print it out character by character in hexadecimal format. The file goes something like this in hexadecimal 0x4d 0x5a 0x90 0x00 0x03 .... so on When I...
8
by: Jim Anderson | last post by:
It's been a few years since I have programmed in C++. I'm starting to develop some programs in C++ and I can't get "Hello, World" working yet. It looks to me like 'cout' in the std library is not...
20
by: Junmin H. | last post by:
Hello, I am trying to print the range of unsigned char and unsigned int. The one for char is working good, gives me a correct output, however the other one for int doesnt, why?? Thanks #include...
5
by: A. Farber | last post by:
Hello, I'm trying to print out a const char* buffer by first printing 16 bytes as hex codes, then printing them again as characters or dots (if they aren't printable) and so on: 20 A5 96 74...
3
by: IanWright | last post by:
I'm having a little trouble with a progress bar that I've got in C++. This works fine in Windows but is not working correctly in Linux, and I'm wondering if anyone can help? const char DONE =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.