473,326 Members | 2,013 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,326 software developers and data experts.

printf(): giving format to an unsigned long questions

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

Mar 14 '06 #1
4 4159
On Tuesday 14 March 2006 21:26, Lathe_Biosas opined (in
<11**********************@u72g2000cwu.googlegroups .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

Mar 14 '06 #2
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

Mar 14 '06 #3
"Vladimir S. Oka" <no****@btopenworld.com> wrote in message
news:dv**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
On Tuesday 14 March 2006 21:26, Lathe_Biosas opined (in
<11**********************@u72g2000cwu.googlegroups .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
Mar 14 '06 #4
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

Mar 15 '06 #5

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

Similar topics

7
by: sunfiresg | last post by:
During an interview, I am asked to answer a question: Printf is a major formatted output function provided by the standard C library. Printf accepts a formatting string followed by a various...
25
by: Joakim Hove | last post by:
Hello, I have code which makses use of variables of type size_t. The code is originally developed on a 32 bit machine, but now it is run on both a 32 bit and a 64 bit machine. In the code...
3
by: nandh_dp | last post by:
When the below program is compiled and executed, #include <stdio.h> #define MASK 0xFFFFFFULL main() { unsigned long long a; unsigned long b, c;
16
by: Jordan Abel | last post by:
I have written this function and was wondering if anyone else could point out if there's anything wrong with it. The purpose is to substitute for printf when in a situation where low-level I/O has...
43
by: Joe Smith | last post by:
#include <stdio.h> int main(void) { unsigned long gcd(unsigned long m, unsigned long n); unsigned long m; unsigned long n; unsigned long t; m = 2520; n = 154;
19
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
6
by: ray.webster | last post by:
Should the following work *if* I have a c99 compiler please? #include <stdio.h> int main(void) { size_t t = 42; # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
43
by: Jrdman | last post by:
someone has an idea on how the printf function is programmed ?
15
by: Andreas Eibach | last post by:
.... but I have an unsigned long value in the printf. This warning came when I used gcc 4.x to compile. .... unsigned long offset = 0; .... Well OK, an "easy" way would be instead of printf...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.