From: Martin Ambuhl User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: en-us, en, de, fr, ru, el, zh MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: Simple program. Strange results. References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Lines: 90 Message-ID: Date: Wed, 27 Aug 2003 10:33:49 GMT NNTP-Posting-Host: 66.90.170.236 X-Complaints-To: abuse AT earthlink DOT net X-Trace: newsread2.news.atl.earthlink.net 1061980429 66.90.170.236 (Wed, 27 Aug 2003 06:33:49 EDT) NNTP-Posting-Date: Wed, 27 Aug 2003 06:33:49 EDT Organization: EarthLink Inc. -- http://www.EarthLink.net To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Kenton W. Mellott wrote: > When compiling the following program with gcc (no switches) almost all the > input variable end up displaying strange results. > > > #include > > int scanf(const char *format, ...); Don't do this. Trust 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 #include #include 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