Syntax
int getchar (void);
It doesn't accept any arguments. Nonetheless, it outputs the characters read as an unsigned char within an integer, and in case of a file error, it returns EOF indicating the end of the file.
Now, we will create multiple programs that utilize the getchar function to receive single characters in the C programming language, and then display them using the putchar function.
Read a single character using the getchar function
Let's explore a program that reads a single character using the getchar function in the C programming language.
Program.c
#include <stdio.h>
#include <conio.h>
void main()
{
char c;
printf ("\n Enter a character \n");
c = getchar(); // get a single character
printf(" You have passed ");
putchar(c); // print a single character using putchar
getch();
}
Output
Enter a character
A
You have passed A
In the program shown above, a user inputs a single character during runtime using the getchar function. Once the character is obtained, it is displayed using the putchar function.
Read n characters from the user using getchar function
Let's explore a program that reads n characters by utilizing the getchar function in the C programming language.
Getchar.c
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int main()
{
char ch;
printf (" Enter a character ( If we want to exit press #) \n");
while (ch != '#') /* accept the number till the user does not enter the # to exit from the loop. */
{
ch = getchar();
printf (" \n We have entered the character : ");
putchar (ch); // print a single character
printf ("\n");
}
return 0;
}
Output
Enter a character ( If we want to exit.. press #)
A
We have entered the character: A
We have entered the character:
B
We have entered the character: B
We have entered the character:
C
We have entered the character: C
We have entered the character:
In the displayed result, a while loop iteratively receives a character from the user until the user inputs the # character. In this scenario, the getchar method extracts a single character from the standard input and assigns it to a variable named ch. Meanwhile, the putchar function displays the retrieved character.
Read a single character using the scanf function
Let's explore a program that reads a character using the scanf function from the C library.
Prog.c
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
printf ("\n Enter the character \n");
scanf ("%c", &ch); // get a single character, numeric or words
printf( " You have entered %c", ch); /* It print a single character or first letter of the words. */
return 0;
}
Output
Enter the character
A
You have entered A
When running the aforementioned code, we observe that it captures either a single character or a sequence of characters through the scanf function as opposed to the getchar function. However, it's important to note a slight distinction; while scanf is capable of receiving either a single character or a group of characters from the user, getchar is limited to accepting only a single character.
Here we once more run the aforementioned program, and on this occasion, it displays the following outcomes.
Enter the character
Apple
You have entered A
Read the characters using a do-while loop
Let's explore a program that reads characters by utilizing a do-while loop along with the getchar function in the C programming language.
Dowhile1.c
#include <stdio.h>
#include <ctype.h>
int main()
{
int ch, i = 0;
char str[150];
printf (" Enter the characters from the keyboard (Press Enter button to stop).\n");
// use do while loop to define the condition
do
{
ch = getchar(); // takes character, number, etc from the user
str[i] = ch; // store the ch into str[i]
i++; // increment loop by 1
} while (ch != '\n'); // ch is not equal to '\n'
printf("Entered characters are %s ", str);
return 0;
}
Output
Enter the characters from the keyboard (Press Enter button to stop).
Well b47gvb come
Entered characters are Well b47gvb come
In the provided code, a do-while loop iteratively receives characters from the user until the user presses the ENTER key to terminate the loop.