You are on page 1of 3

UNIT – 4 Input & Output Functions

C Language

Input and output are the essential operations in C language during program execution. For
generating result, program requires input values and after processing on it will give result or out put
on screen. In program, we can assign values to variable using two methods.

1. We can give value directly to variable during declaration or directly assign value using
assignment operator in any statement of the program e.g. X=10.
2. Method is using standard input function scanf(). There are other functions also available in C
language to accept character value from keyboard. For using different standard library
functions, we required to include appropriate header file for that function before writing
main function.

So each program that uses standard input/output function must contain the statement #
include<stdio.h> at the beginning. In this chapter we will learn about formatted scanf() for input
values and formatted printf() for output values.

There are a number of I/O functions in C, based on the data types. The input/output functions
are classified in two types:
(i) Formatted Functions
(ii) Unformatted Functions

With formatted functions, the input or output is formatted as per our requirement. The readability
in easy way is possible with formatted functions. For example, with formatted functions one can
decide how the result should appear or display on the screen. The result can be shown on the
second line or it can appear after leaving some space or if the result is a real number then decisions
on the number of digits before and after decimal point, etc will be taken care in formatted functions.
However formatting is not possible with unformatted functions.

INPUT AND OUTPUT FUNCTIONS

FORMATTED FUNCTIONS UNFORMATTED FUNCTIONS

printf() getch()
scanf() putch()
getche()
getchar()
putchar()
gets()
puts()

(i) Formatted functions: The formatted input/output functions read and write, respectively, all
types of data values. They require format string to produce formatted results. Hence, they
can be used for both reading and writing of all data values. The formatted functions return
values after execution. The return value is equal to the number of variables successfully
read/written. Using this value, the user can find out the error that occurred during reading
or writing of data. Using this function, the given numeric data can be represented in float,
integer and double to possible available limits of the language.

SDJ International College Page 1


UNIT – 4 Input & Output Functions
The syntax of input function for inputting the data such as scanf() is as follows:
scanf(“control string”, argu 1, argu 2,. . . . );
For example, scanf(“%d”,&x); where %d is a control string which is nothing but conversion
specification and it is to be placed within double quote. The other part is the argument and a
sign &(ampersand) must precede it. Arguments are the identifiers.

For displaying the result, printf() formatted function is used. The list of variables can be
indicated in the printf(). The values of variables are printed according to the sequence
mentioned in printf(). The printf() function prints all types of data values to the console. It
translates internal values to characters. It requires format conversion or format string and
variable names to print the data. The format string symbol and variable names should be the
same in number and type. The syntax of printf() statement is as:

printf(“control string”, variable 1, variable 2…..variable n);


The control string specifies the field format such as %d, %s, %g, %f and variable as taken by
the programmer.

Below table describes the various formats for presenting various outputs:

Sr. Format Meaning Explanation


No.
1 %wd Format for W is width in integer and d is conversion specification
integer output
2 %w.cf Format for float W is width in integer, c specifies the number of digits
numbers after decimal point and f specifies conversion
specification.
3 %w.cs Format for W is width for total characters, c are used for displaying
string output leading blanks and s specifies version specification.

(ii) Unformatted Functions


C has three types of I/O functions:
(a) Character I/O
1. getchar()
this function reads a character typd data from standard input. It reads one character
at a time till the user presses the enter key. The syntax of the getchar() is as follows:
variable name=getchar();
e.g :- char c;
c=getchar();

2. putchar()
This function prints one character on the screen at a time, read by the standard
input. The syntax is as follows:
putchar(variable name);
e.g:- char c=’c’;
putchar( c );

3. getch() & getche()


These functions read any alphanumeric character from the standard input device.
The character entered is not displayed by the getch() function. Syntax is as follows:
getche();
getch();

SDJ International College Page 2


UNIT – 4 Input & Output Functions
4. putch();
This function prints any alphanumeric character taken by the standard input device.
The function getch() reads a keystroke and assigns to the variable ch. The putchar()
displays the character pressed.

(b) String I/o


(a) gets()
This function is used for accepting any string through stdin(keyboard) until enter key
is pressed. The header file stdio.h is needed for implementing the above function.
Format of gets() is as follows:
Char str[length of string in number];
gets(str);

(b) puts()
This function prints the string or character array. It is opposite to gets().
Char str[length of string in number];
gets(str);
puts(str);

(c) cgets()
This function reads string from the console. The syntax is as follows:
cgets(char *st);
It requires character pointer as an argument. The string begins from st[2].

(d) cputs()
This function displays string on the console. The syntax is as follows.
cputs(char *st);

• Commonly used library functions:


1. clrscr()
This function is used to clear the screen. It clears previous output from the
screen and displays the output of the current program from the first line of the
screen. It is defined in conio.h header file. The syntax is as follows:
clrscr();
2. exit()
This function terminates the program. It is defined in process.h header file. The
syntax is as follows:
exit();
3. sleep()
This function pauses the execution of the program for a given number of
seconds. The number of seconds is to be enclosed between parentheses. It is
defined in dos.h header file. The syntax is as follows:
sleep(1);

SDJ International College Page 3

You might also like