Input and Output statement are to take input(read) and output(write) the data in C programming. These are embedded in header file <stdio.h>.
C programming language provides many built-in functions to read any given input and to display data on screen when there is a need to output the result.
There are mainly two of Input/Output functions are used for this purpose. These are discussed as:
Unformatted I/O functions
Formatted I/O functions
Unformatted I/O functions
There are mainly six unformatted I/O functions:
getchar()
putchar()
gets()
puts()
getch()
getchar()
This function is an Input function. It is used for reading a single character from the keyboard. The general syntax is as:
int getchar(void);
A simple C-program to read a single character from the keyboard is as:
/*To read a single character we use getchar() function*/
#include<stdio.h>
void main()
{
char c;
c = getchar();
}
putchar()
This is an output function. It is used to display a single character on the screen. The general syntax is as:
char x;
putchar(x);
A simple program is written as below, which will read a single character using getchar() function and display inputted data using putchar():
/*Use of getchar() and putchar() functions*/
#include<stdio.h>
void main()
{
char x;
x = getchar();
putchar(x);
}
gets()
This is an input function. It is also a buffered function. It will mark null character (‘\0’) in the memory at the end of the string when you press the enter key. The general syntax is as:
gets(c);
A simple C program for the use of gets() function:
/*A program for the use of gets() function*/
#include<stdio.h>
void main()
{
char c[30];
gets(c);
}
puts()
This is an output function. It is used to display a text on the screen for program simplicity. The general syntax is as:
puts("text line");
A program for the use of puts() function:
/*Simple program for the use of puts() with gets() function*/
#include<stdio.h>
void main()
{
char place[25];
puts("Enter the name of place");
gets(place);
puts("Name of the place is:");
puts(place);
The output is as :
Enter the name of place
Lucknow
Name of the place is:
Lucknow
getch()
This is an input function. It is used to read a single character from the keyboard like getchar() function. The character data read by this function is directly assigned to a variable without the need to press the Enter key. The general syntax is as:
c = getch();
A program for the use of getch() function:
/*Program with the use of getch() function*/
#include <stdio.h>
void main()
{
char c;
puts("Enter the Character");
c = getch();
puts("Character is :");
putchar(c);
}
The output is as:
Enter the Character
M
Character is :M
Formatted I/O functions
Formatted I/O functions which refers to an Input or Output data that has been arranged in a particular format. There are two formatted I/O functions :
scanf()
printf()
scanf()
The scanf() function is an input function. It is used to read the mixed type of data from the keyboard. We can read integer, float and character data by using its control codes or format codes. The general syntax is as:
scanf("<list of specifiers>",&<list of variables>);
The scanf() format code (specifier) is as shown in the below table :
Meaning | Format code |
To read a single character | %c |
To read a signed decimal integer (short) | %d |
To read a signed long decimal integer | %ld |
To read a float value exponential | %e |
To read a float value | %f |
To read a double precision float value | %lf |
To read double float value | %g |
To read short integer | %h |
To read an integer (decimal, octal, hexadecimal) | %i |
To read an octal integer only | %o |
To read a hexadecimal integer only | %x |
To read unsigned decimal integer | %u |
To read a string | %s |
printf()
This is an output function. printf() is used to display a text message and to display the mixed type (int, float, char) of data on screen. The general syntax is as:
printf("Message or text line");
OR
printf("control statements",&x1,&x2,&x3,.......&xn);
Example Program:
/*program which shows the use of printf() function*/
#include <stdio.h>
void main()
{
int x;
float y;
char z;
printf("Enter the data");
scanf("%d%f%c",&x,&y,&z);
}