MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
1. MANAGING INPUT AND OUTPUT OPERATIONS
REFERENCE: PROGRAMMING IN C BY BALAGURUSWAMY
MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE
2. The operations which took place in order to take data and make
the display of the processed information are known as input &
output operations.
In C language, in general, user use scanf and printf functions for
input and output purpose respectively. The proper uses of these
functions are termed management of input & output operation.
There are also many functions which are used in C programming.
There exists several functions in âCâ language that can carry out
input output operations. These functions are collectively known
as standard Input/Output Library. Each program that uses
standard input / out put function must contain the statement.
3. Reading a character:
The basic operation done in input output is to read a characters from the
standard input device such as the keyboard and to output or writing it to
the output unit usually the screen.
The getchar function can be used to read a character from the standard
input device. This can also be done with the help of the scanf function.
The getchar has the following form.
variable name = getchar( );
variable name is a valid âCâ variable, that has been declared already and
that possess the type char.
4. # include < stdio.h >
void main ( )
{
char ch;
printf (âType one character:â) ;
ch = getchar() ;
printf (â The character you typed is = %câ, ch) ;
}
5. Writing a character:-
The putchar function which in analogus to getchar function can be
used for writing characters one at a time to the output terminal.
The general form is
putchar (variable name);
Where variable is a valid C type variable that has already been declared
Ex:- putchar(ch);
Displays the value stored in variable C to the standard screen.
6. #include < stdio.h >
void main ( )
{
char in;
printf (â please enter one characterâ);
in = getchar ( ) ;
putchar (in);
}
7. Character test functions
C supports some character test functions that are contained in the file ctype.h and therefore the
statement. #include<ctype.h> must be included in the program.
Function Test
isalnum(c) is c an alphanumeric character
isalpha(c) is c an alphabetic character
isdigit(c) is c a digit
islower(c) is c a lowercase letter
isprint(c) is c a printable character
isupper(c) Is c an uppercase letter
ispunct(c) Is c a punctuation mark
isspace(c) Is c a white space character
8. Formatted Input:
The formatted input refers to input data that has been arranged in a
particular format. Input values are generally taken by using the scanf
function.
The scanf function has the general form:-
scanf (âcontrol stringâ, arg1, arg2, arg3 âŠâŠâŠâŠ.argn);
The format field is specified by the control string and the arguments arg1,
arg2, âŠ...,argn specifies the address of location where address is to be
stored.
The control string specifies the field format which includes format
specifications and optional number specifying field width and the
conversion character % and also blanks, tabs and newlines.
The Blanks tabs and newlines are ignored by compiler. The
conversion character % is followed by the type of data that is to be assigned
to variable of the assignment. The field width specifier is optional.
9. Inputting Integer Numbers:-
The general format for reading a integer number is
% wd
Here percent sign (%) denotes that a specifier for conversion follows
and
w is an integer number which specifies the width of the field of the number
that is being read. The data type character d indicates that the number should
be read in integer mode.
10. Example : scanf (â%3d %4dâ, &sum1, &sum2);
If the values input are 175 and 1342 here value 175 is assigned to sum1
and 1342 to sum 2.
Suppose the input data was follows 1342 and 175.
The number 134 will be assigned to sum1 and sum2 has the value 2
because of %3d the number 1342 will be cut to 134 and the remaining part is
assigned to second variable sum2.
If floating point numbers are assigned then the decimal or fractional part is
skipped by the computer.
To read the long integer data type we can use conversion specifier % ld & % hd
for short integer.
11. Inputting real numbers:
Unlike integer numbers, field specifications are not to be used
while representing a real number therefore real numbers are specified
in a straight forward manner using % f specifier.
The general format of specifying a real number input is
scanf (â% f â, &variable);
Example: scanf (â%f %f % fâ, &a, &b, &c);
With the input data 321.76, 4.321, 678. The values 321.76 is assigned
to a , 4.321 to b & 678 to C.
If the number input is a double data type then the format specifier
should be % lf instead of %f.
12. Inputting character strings:
Single character or strings can be input by using the character
specifiers.
The general format is % wc or %ws
Where c and s represents character and string respectively and w
represents the field width.
The address operator need not be specified while we input
strings.
The specification %s terminates reading at the encounter of a
blank space.
13. Example : scanf (â%c %15sâ, &ch, name):
Some versions of scanf support the following conversion specifications
for strings:-
%[characters]
%[^characters]
The specification %[characters] means that only the characters
specified within the brackets are permissible in the output string. If
the input string contains any other character, the string will be
terminated at the first encounter of such a character.
The specification %[^characters] does not permit the characters
specified after the circumflex(^) in the input string.
14. Reading mixed Data types:-
scanf statement can also be used to input a data line
containing mixed mode data.
For example:
scanf(â%d %c %f %sâ, &count, &code, &ratio, name};
15. Formatted Output:-
Printf function is used to produce the outputs in such a way that they are
understandable and are in an easy to use form.
The general form of printf statement is
printf(âcontrol stringâ, &arg1,&arg2,âŠâŠâŠ.argn);
Control string consists of 3 types of items:
âąCharacters that will be printed on the screen as they appear.
âąFormat specifications that define the output format for display of each item.
âąEscape sequence characters such as n, t and b.
The arguments arg1, arg2,âŠâŠargn are the variables whose values are
formatted and printed according to the specifications of the control string.
16. A simple format specification has the following form:
% w.p type-specifier
Where w is an integer number that specifies the total
number of columns for the output value and
p is another integer number that specifies the number of digits
to the right of the decimal point or the number of characters to
be printed from a string.
A newline can be introduced by the help of a newline character.
For example:- printf (âEnter 2 numbersâ);
17. For example
#include < stdio.h >
main ( )
{
printf (âHello!â);
printf (âWelcome to the world of Engineering!â);
}
Output:
Hello! Welcome to the world of Engineering.
For Example :
printf (âHello!nâ);
OR
printf (ân Welcome to the world of Engineeringâ);
18. Output of integer numbers:-
The format specification for printing an integer number is
%wd
Where
w specifies the minimum field width for the output.
d specifies that the value to be printed is an integer.
19. For example:
printf(â%dâ,9876);
printf(â%6dâ, 9876)
printf(â%2dâ, 9876);
printf(â%-6dâ, 9876);
printf(â%06dâ, 9876);
It is possible to force the printing to be left-justified by placing a
minus sign directly after the % character.
It is also possible to place zeroes before the field width as
shown in the last example.
20. Output of Real Numbers:-
The output of a real number may be displayed in decimal notation using
the following format specification:
% w.pf
The integer w indicates the minimum number of positions that are to
be used for the display of the value and the integer p indicates the number
of digits to be displayed after the decimal point.
The value, when displayed, is rounded to p decimal places and printed right-
justified in the field of w columns. Leading blanks and trailing zeros will
appear as necessary. The default precision is 6 decimal places. The negative
numbers will be printed with the minus sign.
y=98.7654
22. Printing a single character:-
A single character can be displayed in a desired position using
the format:
%wc
The character will be displayed right-justified in the field of w
columns. We can make the display left-justified by placing a minus
sign before the integer w.
The default value for w is 1.
23. Printing of Strings:-
The format specification for outputting strings is similar to
that of real numbers that is
%w.ps
Where w specifies the field width for display and p instructs that
only the first p characters of the string are to be displayed. The
display is right-justified.
For example: Printing the string âNEW DELHI 110001â.
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
6 7 8 9 0
25. Mixed Data Output:-
It is permitted to mix data types in one printf statement.
For example:-
printf(%d %f %s %câ, a, b, c, d);
printf uses its control string to decide how many variables
to be printed and what their types are. Therefore, the
format specifications should match the variables in
number, order, and type.
26. Specifier Meaning
%c â Print a character
%d â Print a Integer
%i â Print a Integer
%e â Print float value in exponential form.
%f â Print float value
%g â Print using %e or %f whichever is smaller
%o â Print actual value
%s â Print a string
%x â Print a hexadecimal integer (Unsigned) using lower case a â F
%X â Print a hexadecimal integer (Unsigned) using upper case A â F
%a â Print a unsigned integer.
%p â Print a pointer value
%hx â hex short
%lo â octal long
%ld â long