Application: Input Using scanf()
Standard I/O function scanf() to read formatted input
scanf() uses a format string like printf()
- requires the address of a variable as argument
- use
%d to read an integer value (%f for float, %lf for double )
#include <stdio.h>
…
int answer;
printf("Enter your answer: ");
scanf("%d", &answer);
|
-
scanf() returns a value — the number of items read
- use this value to determine if
scanf() successfully read a number
-
scanf() could fail e.g. if the user enters letters
|