Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf(): giving format to an unsigned long questions

Reply
Thread Tools

printf(): giving format to an unsigned long questions

 
 
Lathe_Biosas
Guest
Posts: n/a
 
      03-14-2006
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

 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-14-2006
On Tuesday 14 March 2006 21:26, Lathe_Biosas opined (in
<(E-Mail Removed) .com>):

> 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

 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      03-14-2006
Lathe_Biosas wrote:
> 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;
}

--
Peter

 
Reply With Quote
 
Alex Fraser
Guest
Posts: n/a
 
      03-14-2006
"Vladimir S. Oka" <(E-Mail Removed)> wrote in message
news:dv7d1q$aaj$(E-Mail Removed)-infra.bt.com...
> On Tuesday 14 March 2006 21:26, Lathe_Biosas opined (in
> <(E-Mail Removed) .com>):
> > 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


 
Reply With Quote
 
Lathe_Biosas
Guest
Posts: n/a
 
      03-15-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
newbie: format for unsigned long long (c99) TK C Programming 3 12-10-2005 01:37 PM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments