Simple program. Strange results.
Don't do this. Trust <stdio.h> to have the correct prototype.
^^^
The '!' is wrong. You mean `scanf("%f", &x);'
^^^
The '!' is wrong. You mean `printf("you just entered: '%g'.\n", x);
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
|