Input and Output Functions in C - 1622196378713
Input and Output Functions in C - 1622196378713
Every C program performs three main functions i.e. it accepts data as input,
processes data and produces output. Input refers to accepting of data while
output refers to the presentation of data. Normally input is accepted form
keyboard and output is displayed to screen. Input output operations are most
common task in every programming language. C languages has different type of
input output functions for input-output operations.
Syntax:
The format string indicates how many arguments follow and what their types are.
The arguments var1, var2, …, varN are the variables whose values are formatted
and printed according to format specifications of the format string. The
arguments must match in number, order and type with the format specifications.
Example1:
Output:
Here, header file stdio.h is included because the prorotype of function printf() is
defined in this library. So to use printf() in our program we must include stdio.h
using #include directive. Once stdio.h is included then we can use all the function
that are defined in this header file.
Example2:
Output:
In the above program, we first declared variable a as integer using int and b as
floating or fractional number using float. Then we assigned value 2 to variable a
and value 4.5 to variable b. After that in printf() statement everything is printed as
it is but in place of %d value of a is assigned since %d is placeholder for integer
and in place of %f value of b is assigned since %f is placeholder for floating
number. And by default when using %f, six digit after decimal point is printed. We
can control this behaviour by using %0.2f, this tells compiler to print only two digit
after decimal point.
Example3:
Output:
Syntax:
int a;
float b;
scanf("%d%f", &a, &b);
For Example:
Output:
Unformatted input output functions cannot control the format of reading and
writing the data. These functions are the most basic form of input and output and
they do not allow to supply input or display output in user desired format that's
why we call them unformatted input output functions. Unformatted input output
functions are classified into two categories as character input output functions
and string input output functions.
Character input functions are used to read a single character from the keyboard
and character output functions are used to write a single character to a screen.
getch(), getche(), and getchar() are unformatted character input functions while
putch() and putchar() are unformatted character output functions.
Syntax:
character_variable = getch();
Example1:
Output:
While using getch() pressed character is not echoed. Here pressed character is e
and it is displayed by printf().
Syntax:
character_variable = getche();
For Example:
Output:
While using getch() pressed character is not echoed but while using getche() it is
echoed. Here pressed character is e and it is displayed by printf().
getchar() is also a character input functions. It reads a character and accepts the
input until carriage return is entered (i.e. until enter is pressed). It is defined in
header file stdio.h.
Syntax:
character_variable = getchar();
For Example:
Output:
While using getchar() you can enter as many character but only first charcater is
read.
putch() library function:
The putch() function is used for printing character to a screen at current cursor
location. It is unformatted character output functions. It is defined in header file
conio.h.
Syntax:
putch(character);
or
putch(character_variable);
For Example:
Output:
While using getch() pressed character is not echoed. Here pressed character is e
and it is displayed by putch().
putchar(character);
or
putchar(character_variable);
For Example:
Output:
While using getche() pressed character is also echoed. Here pressed character is f
and it is displayed by putchar() later.
Syntax:
gets(string_variable);
For Example:
Output: