Input and Output functions in c
1 minute read
Data Input and Output functions
getchar() function.
getchar() is used to get single character from user at run time.
Declaration:
int getchar();
Example Declaration:
char ch;
ch = getchar();
Example
// Header Files
#include<stdio.h>
#include<conio.h>
//Main Function
void main()
{
char ch;
ch = getchar();
printf(
"Char Is :%c",ch);
}
Output
Char is a
getch() function.
getch() is used to get a character from user but does not display to the screen.
Declaration:
int getch(void);
Example Declaration:
char ch;
ch = getch(); (
or ) getch();
Return Value:
This function return the character which is read from the keyboard.
Example Program:
void main()
{
char ch;
ch = getch();
printf(
"Input Char Is :%c",ch);
}
Program Explanation:
During the program execution, a single character is get from user using the getch(). The character is not displayed on the screen and the compiler does not wait for another character to input from user. And then, the given character is printed by the printf function.
Putchar() function
putchar() displays any number or characters to the standard output device.
It displays only one character at a time.
Declaration:
int putchar(variable_name);
Example Declaration:
char ch =
'a';
putchar(ch);
Example Program:
#include<stdio.h>
#include<conio.h>
//Main Function
void main()
{
char ch=
'a';
putchar(ch);
getch();
}
Output
a
gets() function
Get or Read String Using gets()
gets() accepts single or multiple characters including spaces from the standard Input device.
Declaration:
gets(variable_name);
Example Declaration:
char ch[
10];
gets(ch);
Example Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char a[
20];
gets(a);
puts(a);
getch();
}
Sample Output:
Dhaval sir
Dhaval sir
Puts() function
puts() displays single or multiple characters of string including spaces to the standard output device.
Declaration:
puts(variable_name);
Example Declaration:
char ch[
10]=
"Example";
puts(ch);
Example Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char a[
20];
gets(a);
puts(a);
getch();
}
Sample Output:
Dhaval sir
Dhaval sir