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:”);
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):
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: ch=getch();
where, ch - assigned the character that is returned by getch.
iv) 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: ch=getche();
v) 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( );
vi) putchar
A single character is displayed on the screen.
Syntax: putchar(ch);
vii) putch
This function will display a single character on the screen. The character that is displayed is returned.
Syntax: putch(ch);
where, ch - the character that is to be printed.
viii) gets and puts
gets is used to read a string of any length.
Syntax: gets(String variable);
Puts is used to display a string to the output device.
Syntax: puts("String");
Example:
#include <stdio.h>
void main()
{
char line[30];
gets (line);
puts (line);
}