UMBC CS 201, Fall 05
scanf4
Our fourth example shows that using scanf to read in several
variables can be confusing to the user.
In this program, we try to read in an integer followed by a double.
The results could be confusing to the user because it is not always
clear where the integer ends.
The Program
/********************************************
** File: scanf4.c
** Author: R. Chang
** Modified by: S. Bogar
** Date: 3/5/04
** Section: 01XX & 02XX
** EMail: bogar@cs.umbc.edu
**
** This program shows the return values
** given by scanf.
**********************************************/
#include
int main ( )
{
int n, numRead;
double x ;
printf("Enter an integer and a double: ") ;
numRead = scanf("%d%lf", &n, &x) ;
printf("n = %d, x = %g, numRead = %d\n",
n, x, numRead) ;
return 0;
}
The Sample Run
linux3[98] % a.out
Enter an integer and a double: 12 13.2
n = 12, x = 13.2, numRead = 2
linux3[99] %
linux3[99] % !a
a.out
Enter an integer and a double: 13.215
n = 13, x = 0.215, numRead = 2
linux3[100] %
CSEE
|
201
|
201 F'05
|
lectures
|
news
|
help
Monday, 26-Sep-2005 11:20:20 EDT