[comp.lang.c] sprintf badness

siva@bally.Bally.COM (Siva Chelliah) (01/21/91)

Hi,
     I am running into problems when I mix floats and integers in sprintf.
        1.  How it is handled normally ?  [Do not use "f" in sprintf ?
            multiply by 10 and divide by 10 ?]

Thanks .

siva

main ()
{
  char buf[90];
  int i,j;
  double k;
  sprintf(buf,"%.4d%07.2f%.4d",45,445.34,1234);
  printf("buf  = %s\n",buf);
  sscanf(buf,"%4d%7f%4d",&i,&k,&j);
  printf("i = %d  j = %d  k= %7.2f",i,j,k);
}
------------ output of the above program ----------------
buf  = 00450445.341234
i = 45  j = 1234  k= 8840025108807417000.00

gceych@iago.caltech.edu (Eychaner, Glenn C.) (01/21/91)

siva@bally.Bally.COM (Siva Chelliah) writes...
>     I am running into problems when I mix floats and integers in sprintf.

No. You are running into problems when you mix floats and doubles in scanf
and sprintf.

>  double k;
   ^^^^^^^^
Note that k is declared as double.

>  sscanf(buf,"%4d%7f%4d",&i,&k,&j);
                  ^^^
I believe this should be "%7lf", for a DOUBLE number.  Same mistake is made
in the next line of the program.

>  printf("i = %d  j = %d  k= %7.2f",i,j,k);
                              ^^^^^
Sorry for posting this.....but it illustrates a BAD HABIT of confusing float
with double (a habit I used to have).

Glenn C. Eychaner | "Do you qualify to be alive, or is the limit of your senses
40386 N. Shore Ln `-----. so as only to survive?" -R.Stevens, _Mr.Businessman_
Big Bear City, CA 92314 `-----------------------. !**** G O  N I N E R S ****!
Eychaner%SunCub.Caltech.edu@Xhmeia.Caltech.edu -|- Big Bear Solar Observatory

gwyn@smoke.brl.mil (Doug Gwyn) (01/22/91)

In article <473@bally.Bally.COM> siva@bally.Bally.COM (Siva Chelliah) writes:
>     I am running into problems when I mix floats and integers in sprintf.

No, sprintf() is doing what you intended.  However, you made a mistake
in your use of sscanf(); a %f format specifier must have a corresponding
pointer-to-flaot argument, but you provided a pointer-to-double.

karl@ima.isc.com (Karl Heuer) (01/22/91)

In article <1991Jan21.004401.15257@nntp-server.caltech.edu> gceych@iago.caltech.edu writes:
>>  double k;
>>  sscanf(buf,"%4d%7f%4d",&i,&k,&j);
>                  ^^^
>I believe this should be "%7lf", for a DOUBLE number.

Right.  This should fix the problem.

>Same mistake is made in the next line of the program.
>>  printf("i = %d  j = %d  k= %7.2f",i,j,k);

Wrong.  For printf(), "%f" is correct and "%lf" is illegal.  This unfortunate
asymmetry is a result of history and the default widening rules.

(You'll probably find that "%lf" works on your local implementation of
printf().  But since "%f" also works and is Standard, that's the one to use.
Fix it now, before you port to an implementation that works differently.)

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint

blair_wilson@canrem.uucp (blair wilson) (01/24/91)

SC>Hi,
SC>     I am running into problems when I mix floats and integers in sprintf.
SC>        1.  How it is handled normally ?  [Do not use "f" in sprintf ?
SC>            multiply by 10 and divide by 10 ?]

SC>main ()
SC>{
SC>  char buf[90];
SC>  int i,j;
SC>  double k;
SC>  sprintf(buf,"%.4d%07.2f%.4d",45,445.34,1234);
SC>  printf("buf  = %s\n",buf);
SC>  sscanf(buf,"%4d%7f%4d",&i,&k,&j);
SC>  printf("i = %d  j = %d  k= %7.2f",i,j,k);
SC>}
SC>------------ output of the above program ----------------
SC>buf  = 00450445.341234
          111122222223333
          1111 field 1 - this looks correct, your specifier %.4d, asks
          for a field MINIMUM 4 wide (I would think spaces would be used
          as place holders, but this may be implementation defined)

          2222222 field 2 - also correct.  A float 7 wide with leading
          0's and 2 decimal places (this time you asked for leading 0's
          with the 0 in the format spec.

          3333 field 3 - also correct.  A four digit integer occupying 4
          spaces

          You may want to seperate your fields with a space to make them
          easier to decode, i.e. "\n%.4d  %07.2f  %.4d" will force a
          couple of spaces between each field.

SC>i = 45  j = 1234  k= 8840025108807417000.00
          field k messes up in this case because you need to specify %lf
          for doubles, so its stuffing a float, rather than a double, at
          the address you specify.

Hope this helps

email - blair.wilson@canremote.uucp
---
 ~ DeLuxe}ab #280sa ~ I tried the rest but bought the best!!!!
--
Canada Remote Systems.  Toronto, Ontario
NorthAmeriNet Host

Rob.Doucette@f701.n250.z1.fidonet.org (Rob Doucette) (02/11/91)

>main ()
>{
>  char buf[90];
>  int i,j;
>  double k;
>  sprintf(buf,"%.4d%07.2f%.4d",45,445.34,1234);
>  printf("buf  = %s\n",buf);
>  sscanf(buf,"%4d%7f%4d",&i,&k,&j);
>  printf("i = %d  j = %d  k= %7.2f",i,j,k);
>}
>------------ output of the above program ----------------
>buf  = 00450445.341234
>i = 45  j = 1234  k= 8840025108807417000.00
>--- D'Bridge 1.30/001331
> * Origin: EGSGATE Fidonet<->Usenet Gateway (1:250/98)
>(1:250/416)
Try changing line 5 to read - float k;
You could instead change line 6 to read
sscanf(buf, "%4.d%07.3lf%.4d", 45, 445.34, 1234);
L8R.
--  
EGSGate Fidonet Gateway, Toronto (egsgate.fidonet.org)
...!{uunet, moore, lsuc}!eastern!egsgate