Std-10-Computer-Chapter 12 Using IO Operations
Std-10-Computer-Chapter 12 Using IO Operations
scanf()
scanf() is a function to read data (numeric, character) in a formatted manner.
The scanf() stands for scan formatted. It allows us to input data into particular format like int,
char, float etc.
scanf(“control string”, &variable1, &variable2,…..&variableN);
The control string specifies the data format in which values of variables are to be stored.
Example %d, %c, %f etc
The variable names (variable1, variable2,….. variableN) are preceded by & (ampersand) sign is
known as address of operator in C language and it specified the memory location where the
input given by user is stored.
In C each variable is indentified by its name as well as its memory address.
The C compiler uses memory address for processing whereas we refer memory location by its
name in our program.
The control string is also known as format string.
Control String with different Data Type in scanf() function (Reading integers, real
numbers, character and word)
Control string also specifies the order in which variable values appear in the input.
Each format must be preceded by % sign and a character specifying data type and a character
specifying the data also each variable should be separated by a blank space.
Example: scanf(“%c %d”, &grade, &rno);
First input %c is of type char for character data type and second input %d is of type int data
type for integer data type.
Reading Integers
%d is used to read integer value.
When the Then the compiler reads the data till it encounters the blank space.
control string Example: scanf(“%d %d”, &rno, &percentage);
don’t contain The compiler stops reading the value as soon as it encounters an invalid
width character in particular value.
alongwith If user enter “ 3.14 89” . Here decimal number value (3.14) is not valid for
data type (%d) int data type(%d) so it encounters an invalid character and will be assigned
value 3 to rno and will stop reading value further.
We can also The control string has general format %lp
specify the ‘l’ refer to the field width
field width of a ‘p’ refers to the type of variable.
number to be Example: int marks1,marks2;
read along with scanf(“%2d %4d”,&marks1,&marks2);
data type(%d) %2d means 2 digits value will be allowed to enter in the variable marks1 &
%4d means 4 digits value will be allowed to enter in the variable marks2.
When control string contains width alongwith data type, the c compiler
either reads the number of characters/number specified as width or reads
the data till it encounter the blank space.
During execution of above statement if user enters 70 1234, then it will be
stored as mark1=70 and marks2=1234.
But if the user enters 1234 70, then marks1 will be assigned value 12
because the width of the field is %2d and marks2 will be assigned 34
(unread/remaining part of 1234). The value 70 will remain unused for the
scanf() statement.
Presented by Nuzhat Ibrahim Memon 3
Reading Real Numbers
The scanf() statements reads real numbers using specification “%f”.
If user wants to read a number with double data type then specification “%lf”.
“%Lf” to read long double floating point value.
printf()
The printf() function is used to display formatted output on screen.
printf(“control string”,var1,var2,…..varN);
Here var1, var2,……varN can be variables or constants or expressions.
The control string may contain
set of characters (String) which will be printed on monitor as they appear in control string,
format specifier for each variables and
escape sequence characters like \n(new line), \t(tab), \b(backspace)
The printf() function provides features which can be used to display output with different types
of formatting.