hexadecimal

M

Magix

Hi,

unsigned long hex1;

hex1=0x12345678;
printf("Hex1: %#X",hex1);

Why it only print 0x5678, instead of 0x12345678?

Thanks.
 
M

Martin Ambuhl

Magix said:
Hi,

unsigned long hex1;

hex1=0x12345678;
printf("Hex1: %#X",hex1);

Why it only print 0x5678, instead of 0x12345678?

#include <stdio.h>

int main(void)
{
unsigned long hex1;

hex1 = 0x12345678;
/* mha: corrected incorrect specifier for an unsigned long to be
shown as hex and added missing '\n' */
printf("Hex1: %#lX\n", hex1);
return 0;
}

[output]
Hex1: 0X12345678
 
A

Artie Gold

Magix said:
Hi,

unsigned long hex1;

hex1=0x12345678;
printf("Hex1: %#X",hex1);

Why it only print 0x5678, instead of 0x12345678?
Turn up the warning level on your compiler. the `X' format specifier is
for printing `int's. The variable `hex1' is a long. Use `lX'.

HTH,
--ag
 
M

Magix

Martin Ambuhl said:
Magix said:
Hi,

unsigned long hex1;

hex1=0x12345678;
printf("Hex1: %#X",hex1);

Why it only print 0x5678, instead of 0x12345678?

#include <stdio.h>

int main(void)
{
unsigned long hex1;

hex1 = 0x12345678;
/* mha: corrected incorrect specifier for an unsigned long to be
shown as hex and added missing '\n' */
printf("Hex1: %#lX\n", hex1);
return 0;
}

[output]
Hex1: 0X12345678

I got (non-long printf)X. How come?
 
M

Martin Ambuhl

Magix said:
Magix wrote:

Hi,

unsigned long hex1;

hex1=0x12345678;
printf("Hex1: %#X",hex1);

Why it only print 0x5678, instead of 0x12345678?

#include <stdio.h>

int main(void)
{
unsigned long hex1;

hex1 = 0x12345678;
/* mha: corrected incorrect specifier for an unsigned long to be
shown as hex and added missing '\n' */
printf("Hex1: %#lX\n", hex1);
return 0;
}

[output]
Hex1: 0X12345678


I got (non-long printf)X. How come?

Choose:
1) You incorrectly typed your program
2) You incorrectly invoked your compiler
3) You incorrectly installed your compiler
4) Your compiler is broken
 
D

Darklight

Artie said:
Turn up the warning level on your compiler. the `X' format specifier is
for printing `int's. The variable `hex1' is a long. Use `lX'.

HTH,
--ag
should get this with the above program

help.c: In function `main':
help.c:8: warning: unsigned int format, long unsigned int arg (arg 2)
 
D

Dave Thompson

Turn up the warning level on your compiler. the `X' format specifier is
for printing `int's. The variable `hex1' is a long. Use `lX'.
To be precise, X is for unsigned int, which is apparently 16-bit on
the OP's implementation, and lX unsigned long, which is correct.

Turning up warnings only helps on one compiler I know of, although a
very widespread one. This is not a required diagnostic in the
standard, and not commonly implemented, although rather nice.

(Posting to c.l.c however will get you all kinds of diagnostics no
compiler provides, or likely ever will or can. <G>)

- David.Thompson1 at worldnet.att.net
 

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
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top