Programming Logic
Formatted
Input
Functions
B.Bhuvaneswaran, AP (SG) / CSE
9791519152
bhuvaneswaran@rajalakshmi.edu.in
scanf() Function
The scanf() function is used to input a set of values.
This function inputs any combination of digits, characters and
strings.
Formatted Input Functions Rajalakshmi Engineering College 2
General Form
scanf("control strings", arg1, arg2, . . . argn) ;
Where the control strings specifies the field format in which the
data is to be entered and the arguments arg1,arg2, . . . argn
specify the address of locations where the data is stored.
scanf() reads one data item from the input corresponding to each
argument, skipping white spaces including new lines to find the
next data item.
Formatted Input Functions Rajalakshmi Engineering College 3
Rules
All the format specifiers (control strings) are written within double
quotes.
Each format specifier (control string) must be preceded by the %
sign.
Each numeric variable must be preceded by & symbol except for
array of characters.
There must be a format specifier corresponding to every variable.
The order of the format specifier is same as the order of the
corresponding variables.
Formatted Input Functions Rajalakshmi Engineering College 4
Input of Integer Numbers
The field specification for reading an integer number is:
%d
or
%wd
where the percent sign (%) indicates that conversion specification
follows, w is an integer number that specifies the field width of
the number to be read and d known as data type character,
indicates that the number to be read is in integer mode.
Formatted Input Functions Rajalakshmi Engineering College 5
Examples and Explanations
Consider the following statement:
scanf("%2d %2d %3d", &a, &b, &c);
When this statement is executed, the user has to enter the values
through the standard input device (keyboard).
Suppose the input data items entered be:
1 2 3
The assignment will be:
a=1 b=2 c=3
Formatted Input Functions Rajalakshmi Engineering College 6
Examples and Explanations
If the input data items entered be:
12 34 56
The assignment will be:
a = 12 b = 34 c = 56
If the input data items entered be:
12345678
The assignment will be:
a = 12 b = 34 c = 567
And the remaining digits will be ignored.
Formatted Input Functions Rajalakshmi Engineering College 7
Examples and Explanations
If the input data items entered be:
123 45678
The assignment will be:
a = 12 b=3 c = 456
And the remaining digits will be ignored.
Formatted Input Functions Rajalakshmi Engineering College 8
Input of Real Numbers
The field specification for reading a real number is:
%f
The above notation can be used for both decimal point notation
and exponential notation.
Formatted Input Functions Rajalakshmi Engineering College 9
Example and Explanation
Consider the following statement:
scanf("%f %f %f", &a, &b, &c);
when this statement is executed, the user has to enter the values
through the standard input device (keyboard).
Suppose the input data items entered be:
123.45 12.34E-1 123
The assignment will be:
a = 123.45 b = 1.234 c = 123.0
Formatted Input Functions Rajalakshmi Engineering College 10
Input of Single Character
The field specification for reading single characters is:
%c
or
%wc
Formatted Input Functions Rajalakshmi Engineering College 11
Example and Explanation
Consider the following statement:
scanf("%c %c %c", &a, &b, &c);
when this statement is executed, the user has to enter the values
through the standard input device (keyboard).
Suppose the input data items entered be:
r e c
The assignment will be :
a = 'r' b = 'e' c = 'c'
Formatted Input Functions Rajalakshmi Engineering College 12
Input of Strings
The field specification for reading a strings is:
%s
or
%ws
Formatted Input Functions Rajalakshmi Engineering College 13
Example and Explanation
Consider the following statement:
scanf("%s %s %s", a, b, c);
when this statement is executed, the user has to enter the values
through the standard input device (keyboard).
Suppose the input data items entered be:
God is Love
The assignment will be:
a = "God" b = "is" c = "Love"
Formatted Input Functions Rajalakshmi Engineering College 14
Input of Mixed Mode Data Types
It is possible to use one scanf() statement to input a data line
containing mixed mode data such as integer, float and char.
In such cases, care should be exercised to ensure that the input
data items match the control specifications in order and type.
Formatted Input Functions Rajalakshmi Engineering College 15
Example and Explanation
Consider the following statement:
scanf("%d %s %c %f", &id, name, &gender, &salary);
when this statement is executed, the user has to enter the values
through the standard input device (keyboard).
Suppose the input data items entered be:
101 Bhuvan M 6000
The assignment will be:
id = 101 name = "Bhuvan"
gender = 'M' salary = 6000.0
Formatted Input Functions Rajalakshmi Engineering College 16
Different scanf() Format Codes
Format Specifier Type of Argument Input
%c Character Reads a single character
%d Integer Reads a decimal integer
%e or %E or %f or %g or %G Floating point Reads a floating point value
%hd or %hi Short integer Reads decimal short integer
%hu Short integer Reads decimal unsigned short integer
Reads a decimal or hexadecimal or octal
%i Integer
integer
%ld or %li Long integer Reads decimal long integer
%le or %lf or %lg Double Reads signed double
%Le or %Lf or %Lg Long double Reads signed long double
%lo Long integer Reads an octal long integer
%lu Long integer Reads decimal unsigned long integer
%lx Long integer Reads hexadecimal long integer
%o Octal integer Reads an octal integer
%s Sequence of characters Reads a string
%u Integer Reads an unsigned decimal integer
%x or %X Hexadecimal integer Reads a unsigned hexadecimal integer
%[..] Sequence of characters Reads a string of word(s)
Formatted Input Functions Rajalakshmi Engineering College 17
Thank You