blogger Indonesia

Follow judhyns blog

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\n",i);     printf("The input values %d %d\n",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.

No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |