UMBC CS 201, Spring 02
UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

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: S. Bogar ** Date: 2/2/97 ** Section: 101 ** SSN: 123-45-6789 ** EMail: bogar@cs.umbc.edu ** ** This program shows the return values ** given by scanf. ** **********************************************/ #include <stdio.h> 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 S'02 | lectures | news | help

Thursday, 17-Jan-2002 13:52:24 EST