Scanf() function in C


Scanf() function in C :

Using the C library function scanf() , one can input data . Just like the getchar() function , scanf() allows you to input data the only difference being that scanf() does not input character by character.
The scanf() function reads information from the terminal and stores it at the supplied address . scanf() can be used to enter any combination of digits , characters and strings.

The syntax of the scanf() is
        scanf(“format string” , arg1 ,arg2, …. ,argn);
where format string refers to a string contain certain required formatting information . arg1,arg2,…,argn are the addresses of the data items in the memory of the computer.


The format string comprises individual group of characters ,with one character group for each input data item. Each character group must begin with  a per cent sign(%) ,followed by conversion character which indicates the type of the corresponding data item to be read from the keyboard.

The multiple character group in the format string can be contiguous , or separated by a white space characters . If white space characters are used to separate multiple character groups in the format string , then all consecutive whitespace characters in the input data will be read but ignored.

Example program:
Scanf() in C


Output:


Given scanf() statement  scanf(“%s %d %f”,item,&partno,&cost);
It can be divided as character group(with in double quotation in scanf()) and address of variables (After the double quotations).
In the scanf function , the format string is “%s %d %f” . It contains three character groups:
·       %s - indicates that the first argument item which represents a string.
·       %d - indicates the second argument partno which represents an integer.
·       %f - indicates the third argument cost which represents a floating point value.

scanf() cannot be used to enter a string that includes white spaces characters because it considers the whitespace character as the string terminator.
Therefore that part of the string until the first whitespace is encountered will be assigned to the array.

An example program that illustrates the above point:

 Output:


Thank you...
for more post on computer programming visit : https://iamafutureprogrammer.blogspot.com
  

Previous Post
Next Post
Related Posts

0 comments:

Popular Posts