Professional Documents
Culture Documents
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.
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.
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:
Below table describes the various formats for presenting various outputs:
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 );
(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);