printf(): giving format to an unsigned long questions

L

Lathe_Biosas

Hi

I would like to print on a win cmd console a register value, the value
is an "unsigned long" and have some output like these:

Register: 0x00000000

Tryed with

printf("Register: %#010lx \n", register);
printf("Register: %#08lx \n", register);

but I get 0x000003, only the first 6 values ??? from LSB to MSB,
normally MSBs are cero, but I would like to see them all.

Is there a way to print, hexadecimal, the hole 32 bits and add a nice
0x at the beginning?
I have read some books, googled and couldn't give a solution, any help
or info would be kindly appreciated.

Best Regards
 
V

Vladimir S. Oka

Hi

I would like to print on a win cmd console a register value, the
value is an "unsigned long" and have some output like these:

Register: 0x00000000

Tryed with

printf("Register: %#010lx \n", register);
printf("Register: %#08lx \n", register);

but I get 0x000003, only the first 6 values ??? from LSB to MSB,
normally MSBs are cero, but I would like to see them all.

Is there a way to print, hexadecimal, the hole 32 bits and add a nice
0x at the beginning?
I have read some books, googled and couldn't give a solution, any help
or info would be kindly appreciated.

What's your compiler? Are you telling us all?

On my GCC, for:

#include<stdio.h>

unsigned long reg = 0x12345678;

int main(void)
{
printf("Register: %#010lx \n", reg);
printf("Register: %#08lx \n", reg);
}

I get:

Register: 0x12345678
Register: 0x12345678

Which is, I guess, what you expected.

--
BR, Vladimir

This novel is not to be tossed lightly aside, but to be hurled with
great force.
-- Dorothy Parker
 
P

Peter Nilsson

Lathe_Biosas said:
Hi

I would like to print on a win cmd console a register value, the value
is an "unsigned long" and have some output like these:

Register: 0x00000000

Tryed with

printf("Register: %#010lx \n", register);
printf("Register: %#08lx \n", register);

Minor nit, the space before the newline (\n) is redundant and need not
be output
to a text stream. [This applies to all trailing whitespace on a line
before the new-
line.]
but I get 0x000003, only the first 6 values ??? from LSB to MSB,
normally MSBs are cero, but I would like to see them all.

Please show a complete compilable program that exhibits the problem.
Is there a way to print, hexadecimal, the hole 32 bits and add a nice
0x at the beginning?

#include <stdio.h>

int main(void)
{
unsigned long n = 0xDEADBEEF;
printf("Register: 0x%08lX\n", n);
return 0;
}
 
A

Alex Fraser

Vladimir S. Oka said:
I would like to print on a win cmd console a register value, the
value is an "unsigned long" and have some output like these:

Register: 0x00000000

Tryed with

printf("Register: %#010lx \n", register);
printf("Register: %#08lx \n", register);

but I get 0x000003, only the first 6 values ??? from LSB to MSB,
normally MSBs are cero, but I would like to see them all.
[snip]
On my GCC, for:

#include<stdio.h>

unsigned long reg = 0x12345678;

int main(void)
{
printf("Register: %#010lx \n", reg);
printf("Register: %#08lx \n", reg);
}

I get:

Register: 0x12345678
Register: 0x12345678

Which is, I guess, what you expected.

Try changing reg to (eg) 0x1234. If I do, I get (as I would expect):

Register: 0x00001234
Register: 0x001234

In other words, the "0x" prefix (present due to the # flag) is counted as
part of the field width, but your example forces the field width to be
exceeded.

Changing reg to 0 gives me:

Register: 0000000000
Register: 00000000

This is also as expected; the "0x" prefix is only added when the value is
non-zero. But this apparently isn't what the OP wants; for that the simple
solution is to use "0x%08lx".

Alex
 
L

Lathe_Biosas

Hi

Thank you very much for the answers and explaining
It worked with "0x%08lx", is good to know that # works only when the
value is non-zero
Best Regards
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top