I/O functions
I/O functions
scanf( ) Function:
scanf() function is used to read/input values of variables using the
standared input device (keyboard). It has the following form
For eg. scanf(“%d%d”, &a, &b); to read value of int variables a and b.
printf( ) Function:
printf() function is used to print/display values of variables using
the standared output device (monitor). It has the following form
Q. How can the maximum field width for a data item be specified within
a “scanf” function?
Solution: The maximum field width for a data item within a “scanf”
function can be specified using an unsigned integer indicating the field
width is placed within the control string, between the percent sign (%)
and the conversion character. For example,
scanf(“%3d”,&a);
here the maximum field width for the integer variable a is specified as 3.
The number of characters in the actual data item cannot exceed the
specified field width. Any characters that extend beyond the specified
field width will not be read. Such leftover characters may be incorrectly
interpreted as the components of the next data item.
Q. What happens if an input data item contains more characters than the
maximum allowable field width in a “scanf” function? What if the data
item contains fewer characters?
4
Escape Sequence:
The escape sequence „\b‟ moves the cursor one position to the left of its
current position. „\r‟ takes the cursor to the beginning of the line in which
it is currently placed. „\a‟ alerts the user by sounding the speaker inside
the computer. Form feed(\f) advances the continuous stationery paper
inside the printer to the top of the next page. Characters which are used
as delimiters such as the single quote, double quote, etc., and the
backslash can be printed by preceding them with the backslash. Thus, the
statement:
printf(“He said, \”Let‟s do it!\” ”);
Will be printed as:
He said, “Let‟s do it!”
Assignment Statement:
Equality means the process of checking the value of the left side
variable/expression with the right side variable. The equality operator in
C is = =.
Q. Write a C program to find the sum and product of given two numbers.
Solution:
/* program to find sum and product */
conversion specification
Solution: The program in C to find the sum and product of two numbers
is given below:
Solution:
printf("\nTemperature in centigrade=%.3f",cent);
printf("\n\n\nPress any key to exit...");
getch();
return;
}
Q. Two numbers are input through the keyboard into two locations C and
D. Write a program to interchange the contents of C and D.
Solution:
/* Interchanging of contents of two variable c & d */
#include<stdio.h>
#include<conio.h>
main()
{
int c, d, e;
clrscr();
printf("\n Enter the number at location C: ");
scanf("%d",&c);
printf("\n Enter the number at location D: ");
scanf("%d",&d);
/*Interchanging the contents of two variable using a third variable
as temporary store */
e=c;
c=d;
d=e;
printf("\n New Number at location C=%d",c);
printf("\n New Number at location D=%d",d);
printf("\n\n\n Press any key to exit...");
getch();
return;
}
Solution:
9
getchar( ) Function
getchar( ) function is used to read one charater at a time from the
standard input device(keyboard). It has the following form:
ch= getchar ( );
where ch is a char variable.
Eg. char c;
c=getchar ( );
10
When this function is executed, the computer will wait for a key to be
pressed and assigns the value to the variable when the entre key is
pressed.
putchar( ) Function
putchar( ) function is used to display one charater at a time from the
monitor screen. It has the following form:
putchar (ch );
where ch is a char variable.
Eg. char c=‟M‟;
putchar (c );
When this function is executed, the computer will display the value of
the char variable (i.e. letter M) on the monitor screen.
getch( ) Function
getch( ) function is used to read a charater from the keyboard and it does
not expect the enter key press. It has the following form:
ch= getch( );
where ch is a char variable.
Eg. char c;
c=getch( );
When this function is executed, the computer will wait for a key to be
pressed from the key board. As soon as a key is pressed, the control is
transferred to the next line of the program and the value is assigned to
the char variable. It is to be noted that the key(lettrer) pressed will not
be display on the monitor screen.
putch( ) Function
putch( ) function is used to display a charater on the monitor screen. It
has the following form:
putch(ch );
where ch is a char variable.
Eg. char c=‟M‟;
putch(c );
When this function is executed, the computer will display the value of
the char variable „M‟ on the monitor screen.
11
getche( ) Function
getche( ) function is used to read a charater from the keyboard without
expecting the entre key press. However any key pressed by the user will
be display on the monitor. It has the following form:
ch= getche( );
where ch is a char variable.
Eg. char c;
c=getche( );
When this function is executed, the computer will wait for a key to be
pressed from the key board. The value is assigned to the char variable.
Noted that getche() is similar to getch() expect that getche()
display the key pressed from the keyboard on the monitor screen. The „e‟
at the end of getche() stands for echo. The header file <conio.h>
(console input output header) is included to ues these functions in a
program.
gets( ) Function
gets( ) function is used to read a string of characters including white
space. Note that a string containing white spaces cannot be read using
scanf() with “%s” format specifer. It has the following form:
gets(st );
where st is a char stringvariable.
Eg. char st[20];
gets(st );
When this function is executed, the computer waits for the string value.
puts( ) Function
puts( ) function is used to display a charater string on the monitor screen.
It has the following form:
puts(str );
where str is a string(array of characters).
Eg. char str[20]=‟Computer World‟;
12
puts(str);
When this function is executed, the computer will display the string (i.e.
Computer World) on the monitor screen.
clrscr( ) Function
clrscr( ) function is used to clear the monitor screen. It has the following
form:
clrscr( );
When this function is executed, the previously displayed text/error
messages will be cleared. This is used when a new program is run. It is to
be noted that the header file <conio.h> (console input output header) is
included to use this function in a program.
Solution:
The sizeof Operator: The sizeof operator is used to return the size (in
bytes) of its operand. Is is used in the form:
sizeof(b)
Where b is a variable or a type.
Example:
The following program demonstrates the use of the sizeof operator
to display the size of the different data types in bytes.
#include<stdio.h>
main()
{
printf("\nThe Size of char is %d ",sizeof(char));
printf("\nThe Size of int is %d ",sizeof(int));
printf("\nThe Size of float is %d ",sizeof(float));
printf("\nThe Size of double is %d ",sizeof(double));
return;
}
RUN:
The Size of char is 1
13
Solution:
/* Calculate the volume & surface area of sphere given diameter of the
sphere */
#include<stdio.h>
#define PI 3.14
main( )
{
float radius, volume, area;
printf(“\nEnter the radius of sphere : ”);
scanf(“%f”, &radius);
volume = (4*PI*radius*radius*radius)/3;
area=4*PI*radius*radius;
printf(“Volume of sphere = %f”, volume);
printf(“\n Surface area of sphere = %f”,area);
}
Solution:
/* Calculation of aggregate & percentage marks */
#include<stdio.h>
14
#include<conio.h>
main()
{
int m1,m2,m3,m4,m5,aggr;
float per;
clrscr();
printf("\nEnter marks in 5 subjects: ");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
aggr=m1+m2+m3+m4+m5;
per=(float)aggr/5;
printf("\nAggregate Marks=%d",aggr);
printf("\nPercentage Marks=%.2f",per);
printf("\n\n\nPress any key to exit...");
getch();
return;
}
Q. What are the commonly used input/output function in C? How are they accessed?
Solution: There are a couple of function that provide basic I/O facilities.
Pobably the most common are: getchar() and putchar(). They are defined and
used as follows:
int getchar(void) -- reads a char from stdin
int putchar(char ch) -- writes a char to stdout, returns character written.
int ch;
ch = getchar();
(void) putchar((char) ch);
Related Functions:
int getc(FILE *stream),
int putc(char ch,FILE *stream)
Formatted I/O
We have seen examples of how C uses formatted I/O already. Let's look at this in
more detail.
Printf :
The function is defined as follows:
int printf(char *format, arg list ...) --
prints to stdout the list of arguments according specified format string. Returns
number of characters printed.
The format string has 2 types of object:
ordinary characters -- these are copied to output.
conversion specifications -- denoted by % and listed in the following Table :
terminated by \0
f double/float format -m.ddd...
e,E " Scientific Format
-1.23e002
g,G " e or f whichever
is most compact
% - print % character
&&&&&&&&&&