* Re: printf-problem
1996-01-17 5:00 printf-problem Henrik Wist
1996-01-17 5:00 ` printf-problem Alun Champion
@ 1996-01-17 5:00 ` P.Bennett
1996-01-17 5:00 ` printf-problem The Amorphous Mass
1996-01-17 5:00 ` printf-problem Lawrence Kirby
` (2 subsequent siblings)
4 siblings, 1 reply; 10+ messages in thread
From: P.Bennett @ 1996-01-17 5:00 UTC (permalink / raw)
In article <4dj4g6$t9f@sunsystem5.informatik.tu-muenchen.de>, wist@lam.mw.tu-muenchen.de writes...
>Hi Folks!
>Got a little problem with printing large int's from a c-program.
>Platform: Digital Alpha running DG-UX3.2
>long int zahl; /* which is e.g. 10000500001 */
>printf("%d\n",zahl); /* gives 1410565409, which is definitly wrong */
>Is that a problem of printf() or DG-UX or is it my problem that I don't know
>the right conversion for printf(). Btw, printf("%u",zahl) didn't work either.
>Any suggestions?
You have to tell printf() that you are giving it a long. try
printf("%ld"\n,zahl);
Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
Internet: bennett@triumf.ca | of one another only when one can be
Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: printf-problem
1996-01-17 5:00 printf-problem Henrik Wist
@ 1996-01-17 5:00 ` Alun Champion
1996-01-17 5:00 ` printf-problem P.Bennett
` (3 subsequent siblings)
4 siblings, 0 replies; 10+ messages in thread
From: Alun Champion @ 1996-01-17 5:00 UTC (permalink / raw)
In article <4dj4g6$t9f@sunsystem5.informatik.tu-muenchen.de> wist@lam.mw.tu-muenchen.de (Henrik Wist) writes:
: long int zahl; /* which is e.g. 10000500001 */
: printf("%d\n",zahl); /* gives 1410565409, which is definitly wrong */
Though the FAQ goes into a lot of detail about printf and scanf, but it
sort of misses this one. The closes it gets is in the answer to
"How can I convert numbers to strings (the opposite of atoi)?"
In the answer it says :-
You can obviously use sprintf() to convert long or floating-
point numbers to strings as well (using %ld or %f).
I suppose the answer is there ;')
Anyway the answer is %d is the printf specifier for ints not longs
%ld is the printf specifier for longs. Man pages and/or almost any book
on 'C' could have told you this.
Regards
-A.
--
| A.Champion |
^ permalink raw reply [flat|nested] 10+ messages in thread
* printf-problem
@ 1996-01-17 5:00 Henrik Wist
1996-01-17 5:00 ` printf-problem Alun Champion
` (4 more replies)
0 siblings, 5 replies; 10+ messages in thread
From: Henrik Wist @ 1996-01-17 5:00 UTC (permalink / raw)
Hi Folks!
Got a little problem with printing large int's from a c-program.
Platform: Digital Alpha running DG-UX3.2
Problem:
.
.
.
long int zahl; /* which is e.g. 10000500001 */
printf("%d\n",zahl); /* gives 1410565409, which is definitly wrong */
.
.
.
Is that a problem of printf() or DG-UX or is it my problem that I don't know
the right conversion for printf(). Btw, printf("%u",zahl) didn't work either.
Any suggestions?
Thanks alot,
Henrik
---------------------------------------------------------------------
"Woher soll ich wissen, was ich denke, bevor |Tel.:+49/89/3089598
ich nicht gehoert habe, was ich sage?" |smail: Destouchesstr.71
IRC: |GOOSE| | 80796 Munich
Systemadministrator am Lehrstuhl A fuer Mechanik| Germany
---------------------------------------------------------------------
>>>http://www.lam.mw.tu-muenchen.de/mitarb/wist.html<<<
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: printf-problem
1996-01-17 5:00 ` printf-problem P.Bennett
@ 1996-01-17 5:00 ` The Amorphous Mass
0 siblings, 0 replies; 10+ messages in thread
From: The Amorphous Mass @ 1996-01-17 5:00 UTC (permalink / raw)
On 17 Jan 1996, P.Bennett wrote:
> You have to tell printf() that you are giving it a long. try
> printf("%ld"\n,zahl);
or
printf("%ld\n", zahl);
:)
/**James Robinson***********************
"If a fatal error occurs, the program should not be allowed to continue."
-- Oracle Pro*C User's Guide *************james-robinson@uiowa.edu**/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: printf-problem
1996-01-17 5:00 printf-problem Henrik Wist
1996-01-17 5:00 ` printf-problem Alun Champion
1996-01-17 5:00 ` printf-problem P.Bennett
@ 1996-01-17 5:00 ` Lawrence Kirby
1996-01-18 5:00 ` printf-problem Dik T. Winter
1996-01-18 5:00 ` printf-problem Henrik Wist
1996-01-19 5:00 ` printf-problem Achim Reeh
4 siblings, 1 reply; 10+ messages in thread
From: Lawrence Kirby @ 1996-01-17 5:00 UTC (permalink / raw)
In article <4dj4g6$t9f@sunsystem5.informatik.tu-muenchen.de>
wist@lam.mw.tu-muenchen.de "Henrik Wist" writes:
>long int zahl; /* which is e.g. 10000500001 */
>printf("%d\n",zahl); /* gives 1410565409, which is definitly wrong */
>.
>.
>.
>
>Is that a problem of printf() or DG-UX or is it my problem that I don't know
>the right conversion for printf(). Btw, printf("%u",zahl) didn't work either.
>Any suggestions?
RTFM. 'man printf' will almost certainly tell you that the conversion
specifier for longs is %ld.
--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: printf-problem
@ 1996-01-17 5:00 The Amorphous Mass
0 siblings, 0 replies; 10+ messages in thread
From: The Amorphous Mass @ 1996-01-17 5:00 UTC (permalink / raw)
On 17 Jan 1996, Henrik Wist wrote:
> Hi Folks!
> Got a little problem with printing large int's from a c-program.
> Platform: Digital Alpha running DG-UX3.2
>
> long int zahl; /* which is e.g. 10000500001 */
> printf("%d\n",zahl); /* gives 1410565409, which is definitly wrong */
Try
printf("%ld\n", zahl);
It tells printf() that you want zahl printed as a long int (ref. K&R
II, page 244).
/**James Robinson***********************
"If a fatal error occurs, the program should not be allowed to continue."
-- Oracle Pro*C User's Guide *************james-robinson@uiowa.edu**/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: printf-problem
1996-01-17 5:00 printf-problem Henrik Wist
` (2 preceding siblings ...)
1996-01-17 5:00 ` printf-problem Lawrence Kirby
@ 1996-01-18 5:00 ` Henrik Wist
1996-01-19 5:00 ` printf-problem Achim Reeh
4 siblings, 0 replies; 10+ messages in thread
From: Henrik Wist @ 1996-01-18 5:00 UTC (permalink / raw)
Hello Everybody!
[problem deleted]
Thank you for answering. You were all right, and I am sorry that I
asked such a stupid question. I used the "%ld" stuff in many other
programs before. And I don't know why I didn't try it this time.
Maybe I tried it and it didn't work (for any reason like typos), that's
why I posted to comp.unix.osf.osf1.
Anyway, sorry once more, next time I'll check things three times before
posting. Regards,
Henrik
---------------------------------------------------------------------
"Woher soll ich wissen, was ich denke, bevor |Tel.:+49/89/3089598
ich nicht gehoert habe, was ich sage?" |smail: Destouchesstr.71
IRC: |GOOSE| | 80796 Munich
Systemadministrator am Lehrstuhl A fuer Mechanik| Germany
---------------------------------------------------------------------
>>>http://www.lam.mw.tu-muenchen.de/mitarb/wist.html<<<
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: printf-problem
1996-01-17 5:00 ` printf-problem Lawrence Kirby
@ 1996-01-18 5:00 ` Dik T. Winter
0 siblings, 0 replies; 10+ messages in thread
From: Dik T. Winter @ 1996-01-18 5:00 UTC (permalink / raw)
In article <821915961snz@genesis.demon.co.uk> fred@genesis.demon.co.uk writes:
> In article <4dj4g6$t9f@sunsystem5.informatik.tu-muenchen.de>
> wist@lam.mw.tu-muenchen.de "Henrik Wist" writes:
> >long int zahl; /* which is e.g. 10000500001 */
> >printf("%d\n",zahl); /* gives 1410565409, which is definitly wrong */
> >.
> >
> >Is that a problem of printf() or DG-UX or is it my problem that I don't know
> >the right conversion for printf(). Btw, printf("%u",zahl) didn't work either.
> RTFM. 'man printf' will almost certainly tell you that the conversion
> specifier for longs is %ld.
Alas, the FM will not tell him that the value is too large and does not
fit in the variable zahl, so only the lower 32 bits are retained.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924098
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: printf-problem
1996-01-17 5:00 printf-problem Henrik Wist
` (3 preceding siblings ...)
1996-01-18 5:00 ` printf-problem Henrik Wist
@ 1996-01-19 5:00 ` Achim Reeh
1996-01-22 5:00 ` printf-problem Norman Gaywood
4 siblings, 1 reply; 10+ messages in thread
From: Achim Reeh @ 1996-01-19 5:00 UTC (permalink / raw)
Henrik Wist (wist@lam.mw.tu-muenchen.de) wrote:
: Problem:
: long int zahl; /* which is e.g. 10000500001 */
: printf("%d\n",zahl); /* gives 1410565409, which is definitly wrong */
Solution:
printf("%ld\n",zahl); /* gives 10000500001 */
^^^
%ld is the format specifier for long int output. long ints are 64 bit
under DEC Unix and DEC C. ints (format %d) have only 32 bit.
Achim
--
Achim Reeh, Achim.Reeh@mailer.uni-marburg.de
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: printf-problem
1996-01-19 5:00 ` printf-problem Achim Reeh
@ 1996-01-22 5:00 ` Norman Gaywood
0 siblings, 0 replies; 10+ messages in thread
From: Norman Gaywood @ 1996-01-22 5:00 UTC (permalink / raw)
reeh@bunsen.HRZ.Uni-Marburg.DE (Achim Reeh) writes:
>Henrik Wist (wist@lam.mw.tu-muenchen.de) wrote:
>: Problem:
>: long int zahl; /* which is e.g. 10000500001 */
>: printf("%d\n",zahl); /* gives 1410565409, which is definitly wrong */
>Solution:
> printf("%ld\n",zahl); /* gives 10000500001 */
> ^^^
>%ld is the format specifier for long int output. long ints are 64 bit
>under DEC Unix and DEC C. ints (format %d) have only 32 bit.
OK, what about this one:
-------------------------------Cut----------------------
cat > sprintfbug.c
#include <stdio.h>
main()
{
static char buff[20] = "!!!!!";
int width = 4;
int lprecision = 0;
double val = 10.0;
sprintf( buff,"%*.*f", width, lprecision, val);
printf( ":%s:\n", buff );
}
^D
% cc -o sprintfbug sprintfbug.c
% sprintfbug
: 10!:
% uname -a
OSF1 metz.une.edu.au V3.2 17 alpha
-------------------------------Cut----------------------
The output should be:
: 10:
I found this while trying to get the sc spreadsheet working correctly.
This is not a problem in OSF 2.0
--
Norman Gaywood. Department of Mathematics, Statistics and Computing Science.
University of New England, Armidale, NSW 2351, Australia.
Internet: norm@godel.une.edu.au, phone: +61 67 732412, fax: +61 67 733312
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~1996-01-22 5:00 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1996-01-17 5:00 printf-problem Henrik Wist
1996-01-17 5:00 ` printf-problem Alun Champion
1996-01-17 5:00 ` printf-problem P.Bennett
1996-01-17 5:00 ` printf-problem The Amorphous Mass
1996-01-17 5:00 ` printf-problem Lawrence Kirby
1996-01-18 5:00 ` printf-problem Dik T. Winter
1996-01-18 5:00 ` printf-problem Henrik Wist
1996-01-19 5:00 ` printf-problem Achim Reeh
1996-01-22 5:00 ` printf-problem Norman Gaywood
-- strict thread matches above, loose matches on Subject: below --
1996-01-17 5:00 printf-problem The Amorphous Mass
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox