The scanf Function

scanf

Introduction

The scanf function is used to read information from a standard input device (keyboard). scanf starts with a string argument and may contain additional arguments. Any additional arguments must be pointers (to implement calls by reference).

Program

#include 
main()
{
 int i = 0;
 int k,j=10;
 i=scanf("%d%d%d",&j,&k,&i);
 printf("total values inputted %d
",i);
 printf("The input values %d %d
",j,k);
}

Explanation

  1. Statement A indicates scanf; it is used for inputting values for i, j, k.
  2. You have to use the address of the variable as an additional variable, for example, &i.
  3. The first argument is always a string argument with placeholders.
  4. During execution of scanf, the input is processed and it is matched against the string argument. The process is continued until the matching is complete.
  5. When the first placeholder is encountered in the string argument, a value of the specific type of the first element constitutes a match. It is repeated for each placeholder.
  6. If there are one or more whitespace characters in the first string argument between the placeholders, any sequence of one or more whitespace characters in the input completes a match.
  7. If there are other characters in a string argument, the input should have the same character in the same sequence in order to constitute a match.
  8. Once a match is not found, the function is terminated. Matching fails if the expected input is missing.
  9. scanf returns the number of values that have been succesfully input. In this example, if you type A instead of an integer when you are giving the value for k, then scanf returns only 1, although k gets the value 65 (the ASCII value for A).

Points to Remember

  1. Input can be done using scanf.
  2. For scanf, the address of the variable should be passed.
  3. scanf returns the number of successful inputs.

THE scanf PLACEHOLDERS

Introduction

The scanf placeholder consists of % at the beginning and a type indicator at the end. Apart from that it can have *, a maximum field-width indicator, and a type indicator modifier, for example,

%10.2f,%10d

Program/Example

Type indicators

  • d, i Used for signed integers; the expected argument should be a pointer to int.
  • o Used for unsigned int expected's value. It should be an integer in octal form.
  • U Unsigned integer in decimal form.
  • X, X Unsigned integer in hexadecimal form.
  • E, E, f, g, G Floating-point values.
  • S Character string. It matches a sequence of non-whitespace characters terminated by an end-of-line or end-of-file character. The additional argument should be a pointer to char and should point to an area that is large enough to hold the input string as well as the NULL terminator.
  • C Matches the number of characters according to a specified field-width. If no width is specified then a single character is assumed. The additional argument must be a pointer to char; the area pointed to should be large enough to hold the specified number of characters.
  • N Does not read any input but writes the number of characters so far in the target variable.

Use of *

The * is used to suppress input. For example, with %*d, if your input consists of 5 values and you want to ignore the middle 3 values, you can write:

scanf(" %d %*d %*d%*d %d ", &i, &j)

So, if your input is

10 20 30 40 50

it will get the value 10 and j will get the value 50. This is useful when you are getting the input from a file.

Field-width

It indicates the maximum number of characters that are read into the variables.

Explanation

  1. scanf requires two inputs: the first is a string argument and the second is a set of additional arguments.
  2. You can define how the input is to be taken by using placeholders.


C & Data Structures
C & Data Structures (Charles River Media Computer Engineering)
ISBN: 1584503386
EAN: 2147483647
Year: 2006
Pages: 232

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net