0% found this document useful (0 votes)
51 views

Std-10-Computer-Chapter 12 Using IO Operations

Uploaded by

saratsahu4453
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Std-10-Computer-Chapter 12 Using IO Operations

Uploaded by

saratsahu4453
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 12 Computer

Using I/O Operations Class 10


INTRODUCTION
 All the programming languages allow to read input (known as input operations) and to write
output (known as output operations).
 Input means to read data from any input device like keyboard.
 Output means to write data to output devices like monitor, printer, etc.
 Input/Output operations are more popularly known as I/O operations.
INTRODUCTION TO BUILD-IN FUNCTION
 The C language does not have any build-in statement to perform any input and output
function.
 These data input and output operations are carried out by the standard input/output
build-in library functions.
 These data input and output operations are carried out by the standard input/output build-
in library functions.
 We have used scanf() as a standard input function and printf() as a standard output
function in our programs.
 There are two basic ways to give input to the variable.
 One way is to use assignment. For example, the statement number=5; will give input 5
to the number variable.
 Second way is to read data from user during execution of a program. For this any
built-in functions related to input operations like getchar(), getch(), scanf() etc and
output operation like printf(), putchar(), putc(), puts() etc

INBUILT INPUT FUNCTIONS


 Input in a program can be possible through any of the input devices like keyboard, mouse etc.
 C provides several inbuilt input related functions which are stored in a C library.
 To use any inbuilt function from the c library, there is need to include library of that function in the
beginning of program using #include statement.
#include <stdio.h>
 The name stdio.h stands for standard input-output header file.
 The stdio.h header file contains various input and output operations related functions.
 The statement #include informs the compiler to find stdio.h file and place its contents in the
beginning of our program.
getchar()
 One of the simplest way to read a character in a C program at runtime (at the time of execution)
is to use the getchar() function.
variable_name=getchar();
 Here variable_name can be any valid c variable name with char as a data type.
 The getchar() function has no parameters. It just reads single character input.
 When getchar() function is encountered, the program will wait for the user to press a key.
Presented by Nuzhat Ibrahim Memon 1
getch()
 The getch() works exactly same as getchar() but the difference is that the character keyed in will
not echoed on the screen.
 The function getch() is also used to read a character from the user.
 getch() function is useful when we don’t want to show a character typed by the user on screen
i.e. the character entered by the user will not be visible on screen.
 This function is useful when we don’t want to show a character typed by the user on screen.
getc()
 getc() read a character from a file instead of standard input device.
gets()
 gets() function is used to read a string. (Group/sequence of characters is known as a string)
gets(variable_name);
 The gets() function takes one string variable as an argument. Variable_name is the character
array. Example: char city[30];
 In C, String (character array) are enclosed within double quotes (“ “) and each string ends with a
null character ‘\0’. For eg char city[30]=”Bhuj”; char name[20]=”Nuzhat”;
 During execution of program when gets() is encountered, it will wait for user to enter characters
from the input device.
 The function will read characters until a new line character is entered by the user and then
appends a null character ‘\0’ to the end of string.
 gets() can be used to read multiple characters until user press enter key.
FORMATTED INPUT
 getchar(), getch(), getc() etc can be used to read only single character at a time
whereas gets() can be used to read multiple characters until user press enter key.
 All these functions can be used for characters. If we want to enter the numeric data for
calculation, it will be considered as character by these functions (as they accept characters
only) and no mathematical calculations/operations can be performed.
 This problem can be solved by using the facility of formatted input.

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.

Presented by Nuzhat Ibrahim Memon 2


Example: scanf(“%d”,&rollno);
 Here, name of variable is rollno and say value entered by
the user into it is 33.
 The value entered by the user will be stored in computer
memory (say 2345, this number is just an example, it may
be any valid memory address of computer)
 In C, each variable is identified by its name as well as its memory address.
 Whenever a variable is referred in C program, the C compiler uses memory address for
processing.

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.

Reading Character and Word


 To read a character and characters (string) using in-built function like getchar() and gets().
 Same task of reading a character and a words can be achieved using scanf() statement with %c
and %s specification respectively.
 While reading string using scanf() statement & is not required with the variable name.
Example : char c1,c2;
char city[20];
scanf(“%c %c %s”,&c1,&c2,city); [& (ampersand) is not required in city]
 %s specification terminates its reading when blank space encounters in input.
 If user assign “A B Gandhi nagar” then c1 will be assigned value ‘A’, c2 will be assigned
value ‘B’ and city will be assigned value “Gandhi”. The word “Nagar” will be ignored by
the scanf() statement as %s specification terminates its reading at blank space.
Reading user input with %[ characters ] and %[^ characters ]
 C language scanf() function provides %[characters] use to specify the permissible characters
in the input string.
scanf(%[a-z A-Z]”, name);
 Specification %[a-z A-Z] in scanf() specifies that only a-z, spaces and A-Z are accepted and
stored into variable.
 If user input string contains any character which is not listed in the %[characters] specification
then the string will be terminated at the first occurrence of such character.
 %[^characters] specification in the scanf(), the process of inputting the value is immediately
terminated when any of the character from the list is encountered.
scanf(%[^*]”, name); [here ^ represent not]
Reading Mixed Data
 It is possible to read different types of data (like integer, float, char, string) using a single scanf()
statement. Just make sure that Input data items must match with the control specification of
scanf() in order and data types.
scanf(“%d %f %s %c”,&rollno,&percentage,name,&grade);
If user enter “11 90.55 Vani A”; rollno11, percentage90.55, nameVani, gradeA
If user enter “11 Vani A 90.55” then the system will give an error because user had
entered a characters when compiler was looking for floating point number.
Data Type Corresponding Character
For reading a decimal integer %d
For reading a unsigned integer %u
For reading a short integer %h
For reading a long integer %ld
For reading a character %c
Presented by Nuzhat Ibrahim Memon 4
For reading a string %s
For reading a floating point value %f or %e or %g
For reading a double %lf
For reading a long double %Lf

INBUILT OUTPUT FUNCTIONS


 The computer system performs 3 basic tasks: input, process and output.
 Inbuilt input functions can be used to read data from user.
 Inbuilt output functions can be used for displaying processed data.
 The output can be obtained on output devices like monitor, printer and file.
 The output displayed on the Monitor is considered as a standard output device in C language.
 The inbuilt output functions are available in the <stdio.h> header file.
 The inbuilt output functions are putchar(), puts() and printf().
putchar()
 The function putchar() can be used to write a single character to the standard output device.
putchar(character);
 Character can be of char type variable or can be any valid c language character.
 Character will be displayed on the monitor when this function is executed.
 getchar() function read a single character whereas putchar() function displays it on the screen.
 The putchar() has one limitation that it can display only one character at a time.
puts()
 puts() function is used to display multiple characters (String or character array) on an output
device.
puts(variableName);
 variableName is the character or string.
 The puts() functions writes the content stored into variableName to the monitor till the null
character ‘\0’ is encountered.
 Note: Every string contains a null character at the end but puts() function will not display this
character on screen.
FORMATTED OUTPUT
 The inbuilt output functions such as putchar(), puts() etc do not give formatted output.

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.

Presented by Nuzhat Ibrahim Memon 5


 Syntax: %l.m p
 ‘l’ refers to an integer number specify the total character positions in which the variable
value is to be printed
 ‘m’ is used for specifying number of digits after decimal point (of real number) or
number of characters to be printed from a string variable.
 ‘p’ shows the type of variable.
 Specify value of ‘l’ and ‘m’ are optional
 The printf() function is used to display formatted output on screen.
Data Type Corresponding Character
For printing a decimal integer %d
for printing a Character %c
for printing a floating point value without exponent %f
for printing a floating point value in exponent form %e
for printing a string %s
for printing a unsigned integer %u
for printing a long decimal integer %ld
for printing a double %lf
for printing a long double %Lf
for printing integer in octal form %o
for printing integer in hexadecimal form %x
 By default the printf() statement uses left justification to display the output.
 As per the general specification %l.m p,’ m’ indicates number of digits to be displayed after
decimal point.
 When float variable is displayed, it is rounded to ‘m’ decimal places and prints right justified in
the width of ‘l’ columns.
Statement Output
printf(“%f”,123.456) 1 2 3 . 4 5 6 0 0 0
By default 6 digits are displayed after decimal point
printf(“%8.3f”,123.456) 1 2 3 . 4 5 6
when ‘m’ is specified as 3 so it will print 3 decimal
values and are aligned to right in the width of ‘l’ ie 8
printf(“%8.1f”,123.456) 1 2 3 . 5
When ‘m’ specified as 1 then it will print one decimal
value in length of 8 column width.
printf(“%08.1f”,123.456) 0 0 0 1 2 3 . 5
It will print additional padding characters (zero)
before the actual value of real variable.
printf(“%-8.3f”,123.456) 1 2 3 . 4 5 6
Real number can also be printed with left side
justification using minus sign(-)
printf(“%-8.3e”,123.456) 1 . 2 3 4 e + 0 2
l=10,m=3
so 123.456 will be shifted by 2 from the decimal point
so 1.23456*102
Presented by Nuzhat Ibrahim Memon 6
Character and String using printf() function
 Syntax %l.m p
 ‘l’ is used to specify the field width
 ‘m’ for number of characters to be printed
printf(“%s”,”I like Computer”) I l I k e C o m p u t e r
Will print entire string and hereby the length of the string
is 16 (15 characters + 1 null character)
printf(“%8s”,”I like Computer”) I l I k e C o m p u t e r
Here l=8 whereas actual width of the string is 15. So, the
entire string will be printed as it is.
printf(“%18s”,”I like Computer”) I l i k e C o m p u t e r
Here l=18 whereas actual width of the string is 15, so the
entire string will be printed with right justified in 18
compartments.
printf(“%18.6s”,”I like Computer”) I l i k e
I=18 and m=6 is mentioned so the first 6 characters from
the string will be right aligned in 18 compartments.
printf(“%.6s”,”I like Computer”) i l i k e
Here l is not specified and m is 6. So the first 6 characters
of the string will be printed
printf(“%-18.6s”,”I like Computer”) I l i k e
Minus sign (-) will print the string left justified in 18
compartments.
printf(“%20.19s”,”I like Computer”) I l i k e C o m p u t e r
Here m=19 which is greater than length of actual string (15)
so entire string will be printed right aligned in the 20
compartments.

Presented by Nuzhat Ibrahim Memon 7

You might also like