By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

printf question

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On 11 Jul 2003 04:15:27 -0700, Wenjie wrote:
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

--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}
Nov 13 '05 #2

P: n/a
go****@yahoo.com (Wenjie) wrote (11 Jul 2003) in
news:d2**************************@posting.google.c om / 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).
--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America
Nov 13 '05 #3

P: n/a
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"?
Best,
Amities,
Olivier

Nov 13 '05 #4

P: n/a
Olivier Ramare wrote:

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."

--
pete
Nov 13 '05 #5

P: n/a
go****@yahoo.com (Wenjie) wrote:
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.
Thanks and best regards,
Wenjie


Nov 13 '05 #6

P: n/a
"Olivier Ramare" <ra****@agat.univ-lille1.fr> wrote
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"

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.
Nov 13 '05 #7

P: n/a
On Fri, 11 Jul 2003 22:18:16 GMT, Blah wrote:
"Olivier Ramare" <ra****@agat.univ-lille1.fr> wrote
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'.

--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}
Nov 13 '05 #8

P: n/a
pete wrote:
Olivier Ramare wrote:
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

Nov 13 '05 #9

P: n/a

"Pieter Droogendijk" <gi*@binky.homeunix.org> wrote
On Fri, 11 Jul 2003 22:18:16 GMT, Blah wrote:
"Olivier Ramare" <ra****@agat.univ-lille1.fr> wrote
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.
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'.


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.
Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.