Site hosted by Angelfire.com: Build your free website today!
 City & Guilds 7261/224 Coding and Programming in C II 

SCANF

1 INTRODUCTION

The scanf function provides formatted input conversion. Its format is

   scanf("format-string", arguments);
Format-string contains ordinary characters, which the input must match, and conversion specifications which cause input fields (see Section 2.1) to be converted and assigned to the variables, whose addresses are supplied in arguments. Format-string can also contain whitespace characters which tells scanf to ignore whitespace in the input.

2 CONVERSION SPECIFICATION

A simplified syntax for the conversion specification is

   &type
For the purposes of City & Guilds (C&G Ref: 7261/224, Section 6.1.2) you need to know only how to input numeric variables. This is done using the d and f types:
d read into int argument as decimal integer
f read into float argument as floating point number
Other types are:
i read into int argument as decimal integer, or as hexadecimal if prefixed with 0x or 0X.
o read into int argument as octal integer, with optional leading zero.
x read into int argument as hexadecimal integer, with optional leading 0x or 0X.
2.1 Definition of an Input Field

An input field is defined as a string of characters delimited by whitespace. This means that scanf searches across line boundaries for the next input field.

3 EXAMPLE
   /* main: ask for three integers then display their sum */
   int main(void)
   {
      int i1, i2, i3, sum;

      printf("Enter three integers: ");
      scanf("%d %d %d", &i1, &i2, &i3);
      sum = i1 + i2 + i3;
      printf("The sum is %d\n", sum);
      return 0;

   }  /* main */

Four integer variables are declared. printf prompts the user to enter three integers - the absence of \n means the user's input will follow the prompt 1.

scanf accepts three integers, which are placed in the respective variables. Note that the user can enter three numbers separated by any of the input field delimiters (see Section 2.1) so 1 2 3 is valid input, as is 1\t2\t3 (\t represents the press of the Tab key). The Enter key is a valid delimiter, so the user can press Enter after each input as well.

The next line adds the variables together and assigns the result to sum. printf is then used to display the value of sum.

Note well: in scanf all variable names must be prefixed with & (except array names, which we will not be using in scanf). If you do not use & the compiler will not signal either an error or warning.


EXERCISE 1
1 Type in and run the above example program. When entering the three integer values, use the input field delimiters space, tab, and newline.

2 Write a program, using the scanf function, which assigns numbers to the integer variables days, hours, and minutes and then calculates and displays the total number of minutes involved. Your program should display 5164 when 3 14 4 is entered. More obvious test data include 4 when 0 0 4 entered; 60 when 0 1 0 entered; and 1440 when 1 0 0 entered. Don't worry about input validation 2.

3 Write a program, using the scanf function, which converts Fahrenheit to Celsius. Use the formula C = 5(F - 32) / 9. Prompt the user with a suitable request, and display the calculated Celsius value to two decimal places. Use * for multiply, and / for divide. Afterwards, try using C = (5 / 9) * (F - 32). Why do you think it doesn't work?

1 Some systems may require fflush(stdout) immediately after the print statement to make its output visible.

2 scanf expects its input data to be in a strict format, and can trip over if it isn't. Though not a course requirement, it can be useful to know that scanf returns the number of variables correctly converted. So in the example code, if we tested the return value of scanf and it was not 3, the input was not valid.

Back to C Home Page


Martin O'Brien <martin.o_brien@which.net>

Last update: 9 May 1999

Last update: 9 May 1999