Connecting Tech Pros Worldwide Forums | Help | Site Map

printf question

Wenjie
Guest
 
Posts: n/a
#1: Nov 13 '05
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

Pieter Droogendijk
Guest
 
Posts: n/a
#2: Nov 13 '05

re: printf question


On 11 Jul 2003 04:15:27 -0700, Wenjie wrote:[color=blue]
> Hello,
>
>
> Is printf("%d", var); OK for the unsigned long var = SOME_VAR?
> Or should some other print format be specified?[/color]

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;}
Martin Ambuhl
Guest
 
Posts: n/a
#3: Nov 13 '05

re: printf question


gokkog@yahoo.com (Wenjie) wrote (11 Jul 2003) in
news:d2804eb3.0307110315.1e9dd55d@posting.google.c om / comp.lang.c:
[color=blue]
> Hello,
>
>
> Is printf("%d", var); OK for the unsigned long var = SOME_VAR?[/color]

No,
[color=blue]
> Or should some other print format be specified?[/color]

"%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
Olivier Ramare
Guest
 
Posts: n/a
#4: Nov 13 '05

re: printf question


Dan Pop wrote:
[...][color=blue][color=green]
>>Or should some other print format be specified?[/color]
>
> What does your C book have to say on this issue?[/color]

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

pete
Guest
 
Posts: n/a
#5: Nov 13 '05

re: printf question


Olivier Ramare wrote:[color=blue]
>
> Dan Pop wrote:
> [...][color=green][color=darkred]
> >>Or should some other print format be specified?[/color]
> >
> > What does your C book have to say on this issue?[/color]
>
> That's an interesting question. Because Kernighan and
> Ritchie doesn't say a word on that, am I wrong?[/color]

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
Mr. 4X
Guest
 
Posts: n/a
#6: Nov 13 '05

re: printf question


gokkog@yahoo.com (Wenjie) wrote:
[color=blue]
> Hello,
>
>
> Is printf("%d", var); OK for the unsigned long var = SOME_VAR?[/color]

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.
[color=blue]
> Or should some other print format be specified?[/color]

Yes.[color=blue]
>
> Thanks and best regards,
> Wenjie[/color]

Blah
Guest
 
Posts: n/a
#7: Nov 13 '05

re: printf question


"Olivier Ramare" <ramare@agat.univ-lille1.fr> wrote[color=blue]
> Dan Pop wrote:
> [...][color=green][color=darkred]
> >>Or should some other print format be specified?[/color]
> >
> > What does your C book have to say on this issue?[/color]
>
> 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"?[/color]

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.


Pieter Droogendijk
Guest
 
Posts: n/a
#8: Nov 13 '05

re: printf question


On Fri, 11 Jul 2003 22:18:16 GMT, Blah wrote:[color=blue]
> "Olivier Ramare" <ramare@agat.univ-lille1.fr> wrote[color=green]
> > Dan Pop wrote:
> > [...][color=darkred]
> > >>Or should some other print format be specified?
> > >
> > > What does your C book have to say on this issue?[/color]
> >
> > 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"?[/color]
>
> 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"[/color]

That would be because you're using windows.
[color=blue]
> So it's unlikely that man pages would serve well as the universal
> "bible".[/color]

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;}
Olivier Ramare
Guest
 
Posts: n/a
#9: Nov 13 '05

re: printf question


pete wrote:[color=blue]
> Olivier Ramare wrote:
>[color=green]
>>Dan Pop wrote:
>>[...]
>>[color=darkred]
>>>>Or should some other print format be specified?
>>>
>>>What does your C book have to say on this issue?[/color]
>>
>>That's an interesting question. Because Kernighan and
>>Ritchie doesn't say a word on that, am I wrong?[/color]
>
>
> 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."
>[/color]

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

Blah
Guest
 
Posts: n/a
#10: Nov 13 '05

re: printf question



"Pieter Droogendijk" <gin@binky.homeunix.org> wrote[color=blue]
> On Fri, 11 Jul 2003 22:18:16 GMT, Blah wrote:[color=green]
> > "Olivier Ramare" <ramare@agat.univ-lille1.fr> wrote[color=darkred]
> > > 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"?[/color]
> >
> > 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"[/color]
>
> That would be because you're using windows.[/color]

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.
[color=blue]
>[color=green]
> > So it's unlikely that man pages would serve well as the universal
> > "bible".[/color]
>
> 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'.[/color]

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.


Closed Thread


Similar C / C++ bytes