%x question

C

Carramba

hi!

int digit = 12;
printf("%x\n", digit);
running this code I get 'c' as output? that is the problem?
 
O

osmium

Carramba said:
int digit = 12;
printf("%x\n", digit);
running this code I get 'c' as output? that is the problem?

What problem? You asked for hexadecimal and you got it.
 
J

Jonathan Burd

Carramba said:
hi!

int digit = 12;
printf("%x\n", digit);
running this code I get 'c' as output? that is the problem?

%x is a conversion specifier that makes any of the printf
family of functions to print numbers in hexadecimal.

---

§7.19.6.1#8

o,u,x,X
The unsigned int argument is converted to unsigned octal (o), unsigned
decimal (u), or unsigned hexadecimal notation (x or X) in the style
dddd; the letters abcdef are used for x conversion and the letters
ABCDEF for X conversion. The precision specifies the minimum number of
digits to appear; if the value being converted can be represented in
fewer digits, it is expanded with leading zeros. The default precision
is 1. The result of converting a zero value with a precision of zero is
no characters.

---

<ot>
Decimal Hexadecimal Binary
-------------------------------
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111

Visualize an odometer when reading the above table.
(An odometer records the distance that a vehicle travels.)
Read up on hexadecimal and learn to count in
these number systems.

Decimal -- base-10
Hexadecimal -- base-16
Binary -- base-2

All this means is that decimal has 10 digits
that one can use to represent numbers. Binary uses 2 digits.
Hexadecimal uses 16 digits to represent numbers (0 through F).
Yes, A, B, C, D, E, and F are treated as digits in hex.

The last digit in decimal is 9, that in binary is 1, and
that in hexadecimal is F. "10" in any number system
always comes after the last digit. This means
decimal 10 is not the same as hex 10 or binary 10.

Remember never to mix number systems in operations.

Suggested reading:
http://mathworld.wolfram.com/Base.html
http://mathworld.wolfram.com/Hexadecimal.html
http://mathworld.wolfram.com/Binary.html
</ot>

Regards,
Jonathan.
 
N

Nick Austin

int digit = 12;
printf("%x\n", digit);
running this code I get 'c' as output? that is the problem?

What do you want it to output?

I'd write it as:
printf("0x%02X\n", digit);
which produces:
0x0C

Nick.
 
M

Michael Mair

Nick said:
What do you want it to output?

I'd write it as:
printf("0x%02X\n", digit);
which produces:
0x0C

Note: In C99, the '#' flag can be used to obtain alternate forms
of display; especially, octal numbers start _always_ with at least
one leading '0' and hexadecimal numbers start with leading "0x"
or "0X", i.e.
printf("%#02x\n", digit);
printf("%#02X\n", digit);
are possible alternatives.

Cheers
Michael
 
E

Eric Amick

Note: In C99, the '#' flag can be used to obtain alternate forms
of display; especially, octal numbers start _always_ with at least
one leading '0' and hexadecimal numbers start with leading "0x"
or "0X", i.e.
printf("%#02x\n", digit);
printf("%#02X\n", digit);
are possible alternatives.

Two points:

1) This works in C89/C90 as well.
2) The hex conversions print a plain 0 when the value is zero,
regardless of the # flag.
 
M

Michael Mair

Eric said:
Two points:

1) This works in C89/C90 as well.

Thanks -- somehow, I was of the (mistaken) opinion that # had come
in with C99.
2) The hex conversions print a plain 0 when the value is zero,
regardless of the # flag.

Once again, thank you :)


Cheers
Michael
 
M

Mike Wahler

Carramba said:
hi!

int digit = 12;
printf("%x\n", digit);
running this code I get 'c' as output? that is the problem?

No, it's not a problem. The output is correct. '%x'
tells 'printf()' to output the corresponding argument
in hexadecimal notation, in which decimal 12 == hex c.
(note: if you want the alpha hex digits in upper case,
use '%X')

If you want decimal output, use '%d' for type 'int'.

Which C book(s) are you reading?

-Mike
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top