Formatted input/output function in Turbo C Language

It is very easy to use unformatted input/output functions as discussed. We can read and display only characters not numbers using unformatted functions. Using formatted input/output functions, we can read integers, floating point number and characters. The formatted input function is scanf() and formatted output functions is printf().


int scanf(“format string”, &var1, &var2, &var3,……..&varn);

This function scanf() is used to read any type of data such as int, char, float, double and even string from the keyboard. It accepts the following two parameters:
format string consists of one or more format specifires starts with % symbol.
&var1, &var2, &var3, …., &varn represents the list of variables. Here, symbol ‘&’ refers to the address of the variable. In the memory locations identified by these addresses, the respective values are stored.

Formatted input/output function in Turbo C Language

Return Type int: The function returns an integer value. This integer value is the number of item that have been read successfully using the keyboard. But, we rarely use this return value in our programs.
The various format specifiers along with the associated meaning are shown in the following table:

Data Type
Format Specifier
Read a decimal integer value
Read an octal value
Read a hexadecimal value
Read a decimal, octal or hexadecimal value
Read an unsigned integer value
Read a short integer value
Read a floating point number
Read a floating point number
Read a floating point number
Read a character
Read a string (series of character)
Read a long floating point number or double
long int
Read a long integer value

The following table shows the way the user has to enter the values from the keyboard:

Input statement
Entering value
Input way
3 integer value
10 20 30
Where, a=10      b=20       c=30
3 integer value
Where, a=10      b=20       c=30
1 floating point value
Where, a=787.3.5
1 character value
Where, a=p
1 floating point value
1 integer value
1 character value
787.35 10 P
Where, a=787.35, b=10, c=P
3 integer values separated by ‘-‘ (hyphen) sign
Where, a=10, b=20, c=30
3 integer values separated by ‘/’ (slash) symbol
Where, a=10, b=20, c=30

Note: Do not use any escape sequences such as \t, \n etc. in scanf().

For example: Consider the following statement:
In the above statement, remove ‘\n’, otherwise it may give wrong output.
Now, let us see, some more ways of data reading using scanf() function are given below:
Input statement
Entering value
Input way
3 integer values
1 2 3
Where, a=1  b=2  c=3
11 22 33
Where, a=11  b=22  c=33
Where, a=10  b=20  c=30
102 030 4
Where, a=10  b=2  c=3
The remaining two digits 0 and 4 are ignored or can be read by a subsequent scanf()
1 string
Where, str=.Programming
Input the string till the end of line
Wlecome! To the dotprogramming
Where, str= Wlecome! To the dotprogramming
To read a string with space gets() function will used in place of scanf().
The “scanf(“%[^\n]s”,str);” function read and stored all the character untill user will not press enter key.

Formatted Output Function


Popular posts from this blog

difference between structure and union in C Language

Difference between Linear search and Binary Search in c language

Difference between static and dynamic websites