#include <stdio.h> int scanf(const char *format, loc1, ... );
scanf
reads formatted data from stdin
. Following the format in
the argument list may be one or more additional pointers
(loc1, loc2, ..., locn
) addressing storage where the input values will
be stored.
The string pointed to by format
is in the same form as that used by
fscanf
. Refer to the fscanf
description for detailed information
concerning the formatting conventions.
scanf
returns EOF
if end of file (or an input error) occurs before
any values are stored. If any values are stored, it returns the number of items
stored; that is, it returns the number of times a value is assigned by one of the
scanf
argument pointers.
EOF
is returned if an error occurs before any items are matched.
scanf
is identical to fscanf
with stdin
as the input file.
#include <lcio.h> #include <stdio.h> double point[40]; main() { int index = 0; double sum = 0.0; double avg; int nopoints; int stdn_fn = 0; /* If stdin is the terminal, fileno(stdin) is always 0. */ if (isatty(stdn_fn)) /* Tell user to enter data points; maximum = 39. */ puts("Enter data points (-1 to indicate end of list)."); for(;;){ /* Read number; check for end of file. */ if (scanf("%le", &point[index]) <= 0) break; if (point[index] == -1) break; sum += point[index]; ++index; } nopoints = index; avg = sum / nopoints; printf("%d points read.n", nopoints); printf("%f = average.n", avg); }
fscanf
, sscanf
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.