There are some library functions which are available for transferring the information between the computer and the standard input and output devices.
These functions are related to the symbolic constants and are available in the header file.
Some of the input and output functions are as follows:
i) printf
This function is used for displaying the output on the screen i.e the data is moved from the computer memory to the output device.
Syntax:
printf(“format string”, arg1, arg2, …..);
In the above syntax, 'format string' will contain the information that is formatted. They are the general characters which will be displayed as they are .
arg1, arg2 are the output data items.
Example: Demonstrating the printf function
printf(“Enter a value:”);
- printf will generally examine from left to right of the string.
- The characters are displayed on the screen in the manner they are encountered until it comes across % or \.
- Once it comes across the conversion specifiers it will take the first argument and print it in the format given.
ii) scanf
scanf is used when we enter data by using an input device.
Syntax:
scanf (“format string”, &arg1, &arg2, …..);
The number of items which are successful are returned.
Format string consists of the conversion specifier. Arguments can be variables or array name and represent the address of the variable. Each variable must be preceded by an ampersand (&). Array names should never begin with an ampersand.
Example: Demonstrating scanf
int avg;
float per;
char grade;
scanf(“%d %f %c”,&avg, &per, &grade):
- scanf works totally opposite to printf. The input is read, interpret using the conversion specifier and stores it in the given variable.
- The conversion specifier for scanf is the same as printf.
- scanf reads the characters from the input as long as the characters match or it will terminate. The order of the characters that are entered are not important.
- It requires an enter key in order to accept an input.
iii) getch
This function is used to input a single character. The character is read instantly and it does not require an enter key to be pressed. The character type is returned but it does not echo on the screen.
Syntax:
int getch(void);
ch=getch();
where,
ch - assigned the character that is returned by getch.
iv) putch
this function is a counterpart of getch. Which means that it will display a single character on the screen. The character that is displayed is returned.
Syntax:
int putch(int);
putch(ch);
where,
ch - the character that is to be printed.
v) getche
This function is used to input a single character. The main difference between getch and getche is that getche displays the (echoes) the character that we type on the screen.
Syntax:
int getch(void);
ch=getche();
vi) getchar
This function is used to input a single character. The enter key is pressed which is followed by the character that is typed. The character that is entered is echoed.
Syntax:
ch=getchar;
vii) putchar
This function is the other side of getchar. A single character is displayed on the screen.
Syntax:
putchar(ch);
viii) gets and puts
They help in transferring the strings between the computer and the standard input-output devices. Only single arguments are accepted. The arguments must be such that it represents a string. It may include white space characters. If gets is used enter key has to be pressed for ending the string. The gets and puts function are used to offer simple alternatives of scanf and printf for reading and displaying.
Example:
#include <stdio.h>
void main()
{
char line[30];
gets (line);
puts (line);
}