Chapter Contents

Previous

Next
scanf

scanf



Read Formatted Data from the Standard Input Stream

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
DIAGNOSTICS
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdio.h>

int scanf(const char *format, loc1, ... );


DESCRIPTION

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.


RETURN VALUE

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.


DIAGNOSTICS

EOF is returned if an error occurs before any items are matched.


IMPLEMENTATION

scanf is identical to fscanf with stdin as the input file.


EXAMPLE

#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);
}


RELATED FUNCTIONS

fscanf , sscanf


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.