printf question

W

Wenjie

Hello,


Is printf("%d", var); OK for the unsigned long var = SOME_VAR?
Or should some other print format be specified?


Thanks and best regards,
Wenjie
 
P

Pieter Droogendijk

Hello,


Is printf("%d", var); OK for the unsigned long var = SOME_VAR?
Or should some other print format be specified?

printf ("%lu", var);

snipped man printf:
l A following integer conversion corresponds to a long int or
unsigned long int argument
u The unsigned int argument is converted to unsigned decimal
 
M

Martin Ambuhl

(e-mail address removed) (Wenjie) wrote (11 Jul 2003) in
/ comp.lang.c:
Hello,


Is printf("%d", var); OK for the unsigned long var = SOME_VAR?
No,

Or should some other print format be specified?

"%lu" The 'l' because it is a long (if '%d' is a long, it's an
accident of the implementation used) and the 'u' because it's
unsigned ('%d' isn't).
 
O

Olivier Ramare

Dan Pop wrote:
[...]
What does your C book have to say on this issue?

That's an interesting question. Because Kernighan and
Ritchie doesn't say a word on that, am I wrong?
Then "man printf" doesn't go that far either, and
the info program being most probably wrongly installed
by Suse, "info printf" says it can't find any node
called "Top".... Well in fact, "man 3 printf" is
excellent.
So to whom should we have asked this question? Or more
precisely, where is the bible hidden? Always through
"man"?
Best,
Amities,
Olivier
 
P

pete

Olivier said:
Dan Pop wrote:
[...]
What does your C book have to say on this issue?

That's an interesting question. Because Kernighan and
Ritchie doesn't say a word on that, am I wrong?

Yes, you are wrong.

Section 1.2 Variables And Arithmetic Expressions page 11

"Each % construction in the first argument of printf is paired
with the corresponding second argument, third argument, etc.;
they must match up properly by number and type,
or you will get wrong answers."
 
M

Mr. 4X

Hello,


Is printf("%d", var); OK for the unsigned long var = SOME_VAR?

No - the call would 'pick' an int FROM THE UNSIGNED LONG i.e. access the
unsigned long as an int. That could result in garbage values.
Or should some other print format be specified?

Yes.
 
B

Blah

Olivier Ramare said:
Dan Pop wrote:
[...]
What does your C book have to say on this issue?

That's an interesting question. Because Kernighan and
Ritchie doesn't say a word on that, am I wrong?
Then "man printf" doesn't go that far either, and
the info program being most probably wrongly installed
by Suse, "info printf" says it can't find any node
called "Top".... Well in fact, "man 3 printf" is
excellent.
So to whom should we have asked this question? Or more
precisely, where is the bible hidden? Always through
"man"?

Hmmmm.... I just tried "man printf", "man 3 printf" and "info printf" and
every one of them got the same result:

"Bad command or file name"

So it's unlikely that man pages would serve well as the universal "bible".
OTOH, K&R has a quite a bit to say about printf, and in particular includes
both details about what to use for unsigned and what to modify that with for
long, and also has a warning that printf doesn't work right when your
conversion characters don't match your arguments. Perhaps you should review
it.
 
P

Pieter Droogendijk

Olivier Ramare said:
Dan Pop wrote:
[...]
Or should some other print format be specified?

What does your C book have to say on this issue?

That's an interesting question. Because Kernighan and
Ritchie doesn't say a word on that, am I wrong?
Then "man printf" doesn't go that far either, and
the info program being most probably wrongly installed
by Suse, "info printf" says it can't find any node
called "Top".... Well in fact, "man 3 printf" is
excellent.
So to whom should we have asked this question? Or more
precisely, where is the bible hidden? Always through
"man"?

Hmmmm.... I just tried "man printf", "man 3 printf" and "info printf"
and every one of them got the same result:

"Bad command or file name"

That would be because you're using windows.
So it's unlikely that man pages would serve well as the universal
"bible".

Hard to dismiss it if you've never even heard about it before. Maybe you
should go look for yourself first: http://www.sonic.net/cgi-bin/man .
Try '3 printf'.
 
O

Olivier Ramare

pete said:
Olivier said:
Dan Pop wrote:
[...]
Or should some other print format be specified?

What does your C book have to say on this issue?

That's an interesting question. Because Kernighan and
Ritchie doesn't say a word on that, am I wrong?


Yes, you are wrong.

Section 1.2 Variables And Arithmetic Expressions page 11

"Each % construction in the first argument of printf is paired
with the corresponding second argument, third argument, etc.;
they must match up properly by number and type,
or you will get wrong answers."

Ok, for this aspect, but it is quite natural. The fact is that I
didn't find any %l was also a stupidity... !!! The conversion table 7.1
doesn't speak about %lu but the short explanation before does...
So now I have my bible! Thanks !
Amities,
Olivier
 
B

Blah

Pieter Droogendijk said:
Olivier Ramare said:
Dan Pop wrote:
[...]
Or should some other print format be specified?

What does your C book have to say on this issue?

That's an interesting question. Because Kernighan and
Ritchie doesn't say a word on that, am I wrong?
Then "man printf" doesn't go that far either, and
the info program being most probably wrongly installed
by Suse, "info printf" says it can't find any node
called "Top".... Well in fact, "man 3 printf" is
excellent.
So to whom should we have asked this question? Or more
precisely, where is the bible hidden? Always through
"man"?

Hmmmm.... I just tried "man printf", "man 3 printf" and "info printf"
and every one of them got the same result:

"Bad command or file name"

That would be because you're using windows.

Exactly my point. Not every machine has man pages, and more
importantly, those that do don't always make a clear distinction between
what is standard and what is implementation specific. While they tend to be
excellent sources of information (though occasionally remarkably spare on
some topics), they certainly shouldn't be considered a universal C bible.
They're great so long as you know their limitations.
Hard to dismiss it if you've never even heard about it before. Maybe you
should go look for yourself first: http://www.sonic.net/cgi-bin/man .
Try '3 printf'.

On the Solaris systems that I do most of my work on man -s3 printf is
just fine, no need for me to check a website to get them.
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top