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

printf question

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
9 3038
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
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
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
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
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
"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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Mantorok Redgormor | last post by:
Would this by any chance invoke undefined behavior? extern int printf(const char *, ...); int main(void) { printf("Hello\n"); return 0; } That is, providing my own printf prototype would...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
12
by: John Devereux | last post by:
Hi, I would like to know if their is a conversion specifier, or other method, that will allow me to not use particular arguments to printf. Failing that, is it permissable to simply "not use"...
11
by: rahul8143 | last post by:
Does printf output depends on compilers or operating systems? I have one program int main() { int i=5; printf("%d %d %d",i++,i++,i++); return 0; } Output of this program on DOS (TC) 7 6 5...
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
7
by: Martin Jørgensen | last post by:
Hi, Perhaps a stupid question but I wonder if this isn't possible using C: printf("\nOr push <enter> for default (which is theta=%ld)%s", theta, ": "); It would be nicer to have it on one...
5
by: Why Tea | last post by:
int printf(const char *fmt,...) The fmt is const, indicating that it can't be changed during runtime. If I want to print a series of integers that I only know the largest value beforehand,...
7
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.