1 2 3 4 5 6

Formatted Input and Output
Example
int i, n;
float f;
double d;
char str[30];

n = scanf("%d %f %lf, %s", &i, &f, &d, &str[0]);

* *

If this call is successful the value of n will be 4 This format expects to see one integer, two floating point numbers and a string.  The first floating point number is stored in a float and the second one is stored in a double--weused %lf as the format
If we didn't use the %lf format for d, the value stored in d would be incorrect
All the parameters have an & so we are passing a pointer instead of the value.  We could replace &str[0] by str, since str is already a pointer--and this is often done.

*

In addition to scanf() and printf() we have the prototypes:
int fscanf (FILE* fp, const char* format, *arg1, *arg2, ... );
int fprintf (FILE* fp, const char* format, expr1, expr2, .. );

FILE* fp = fopen ("tally.data", "r");

n = fscanf (fp, "%d %f %lf, %s", &i, &f, &d, str);

1 February, 1998

2

CopyrightUniversity of Alberta