delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/08/27/21:15:43

From: "Kenton W. Mellott" <melken AT co DOT tds DOT net>
Newsgroups: comp.os.msdos.djgpp
References: <HfZ2b.3213$cQ1 DOT 755278 AT kent DOT svc DOT tds DOT net> <hO%2b.16285$8i2 DOT 341 AT newsread2 DOT news DOT atl DOT earthlink DOT net>
Subject: Re: Simple program. Strange results.
Lines: 116
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <tHc3b.3480$cQ1.829301@kent.svc.tds.net>
Date: Thu, 28 Aug 2003 01:14:01 GMT
NNTP-Posting-Host: 69.21.9.21
X-Complaints-To: abuse AT tds DOT net (TDS.NET Help Desk 1-888-815-5992)
X-Trace: kent.svc.tds.net 1062033241 69.21.9.21 (Wed, 27 Aug 2003 20:14:01 CDT)
NNTP-Posting-Date: Wed, 27 Aug 2003 20:14:01 CDT
Organization: TDS.NET Internet Services www.tds.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Thanks for the corrections!  DJGPP's help gave no reason for the ampersand.
But if I had be wise enough to read the tutorial for scanf  I had already
downloaded for 'The Art of Computer Programming'; I would have noted that C
uses it to express passing the address for the variable, instead of the
variable contents itself.

  From what I can grasp, the asterisk symbol means the same thing.  Though I
have yet to really grasp the proper place(s) and form(s) for its usage.

Sincerely,


Gregory D. MELLOTT


"Martin Ambuhl" <mambuhl AT earthlink DOT net> wrote in message
news:hO%2b.16285$8i2 DOT 341 AT newsread2 DOT news DOT atl DOT earthlink DOT net...
> Kenton W. Mellott wrote:
>
> > When compiling the following program with gcc (no switches) almost all
the
> > input variable end up displaying strange results.
> >
> >
> >         #include <stdio.h>
> >
> >         int scanf(const char *format, ...);
>
> Don't do this.  Trust <stdio.h> to have the correct prototype.
>
> >
> >         int main()
> >         {
> >                 puts("Please enter a string.");
> >                 char buf[100];
> >                 scanf("%s", buf);
> >                             printf("you just entered: ''%s''.  \n",
buf );
> >
> >
> >                 puts("Please enter a floating point number.");
> >                 float x,y;
> >                 scanf("%f", !x);
>                                ^^^
> The '!' is wrong.  You mean `scanf("%f", &x);'
>
> >                             printf("you just entered: '%g'.  \n", !x );
>                                                                      ^^^
> The '!' is wrong.  You mean `printf("you just entered: '%g'.\n", x);
>
> >
> >
> >                 puts("Please enter 2 floating point numbers and a
string.");
> >                 scanf("%f %f %s", !x, !y, buf);
> >                             printf("you just entered: '%g','%g',
'%s'.\n",
> > !x, !y, buf);
>
> As before, all the '!'s are wrong.
>
> >
> >
> >  }
>
> Please use spaces instead of tabs when preparing code for posting.  You
can
> see above what a mess you got.
>
> Here's a version that is better formatted, will compile under either C89
or
> C99 (as well as gnu89 and gnu99), and takes "enter a string" more
seriously:
>
> #include <stdio.h>
> #include <string.h>
> #include <ctype.h>
>
> int main()
> {
>      char buf[100], *nl;
>      float x, y;
>      int nchar;
>      puts("Please enter a string.");
>      fgets(buf, sizeof buf, stdin);
>      if ((nl = strchr(buf, '\n')))
>          *nl = 0;
>      printf("you just entered: \"%s\".\n", buf);
>
>
>      puts("Please enter a floating point number.");
>      fgets(buf, sizeof buf, stdin);
>      sscanf(buf, "%f", &x);
>      printf("you just entered: '%g'.\n", x);
>
>
>      puts("Please enter 2 floating point numbers and a string.");
>      fgets(buf, sizeof buf, stdin);
>      if ((nl = strchr(buf, '\n')))
>          *nl = 0;
>      sscanf(buf, "%f %f %n", &x, &y, &nchar);
>      printf("you just entered: '%g','%g', \"%s\".\n", x, y, buf + nchar);
>      return 0;
> }
>
>
>
>
>
>
>
> -- 
> Martin Ambuhl
>


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019