Before performing any C program, you need to make sure that your system is upgraded and up to date. After that, it’s time to use the getchar() function in the C program. To do so, we require a C file. If you don’t have one, try creating a C-type file with Ubuntu’s “touch” instruction on the shell. This file must contain the “c” extension with its name. We have been creating this file in Ubuntu’s “home” directory. You can use another folder as well. After this, we have to open this newly generated C-type file so that we can add some C code to it. Usually, Linux users prefer to open such files in the built-in text editor. You can use it as well. But, we have been using the GNU Nano editor here so far using the “nano” instruction as shown.
The empty file will be opened on your Ubuntu screen and ready to be used. We need to add the standard input-output header library of C in this code file so that we can make use of standard input and output functions and statements within the code without any issue. Overall all the C code implementation will be done within the built-in main() method as all the execution starts from this method as well. So, we will be avoided to use the other user-defined functions for our examples. We have started the main() function with its integer return type and declared a character type variable “var” which has not been initialized yet.
We have been utilizing the C’s “printf” statement to print out “Enter a character” on our Linux shell screen. This will be an indication to a user that he/she has to put some character value as an input. On the very next line, we have been using our topic function “getchar()” to get the character value from our user as an input and store it to the already defined variable “var”. The next printf statement is used to display the “Character” string i.e., before the input character as an indication. Now, to display the inputted character value on our shell, we have to use the “putchar()” function of “c” taking the variable “var” as an argument. As the getchar() and putchar() functions work with each other, we cannot avoid “putchar()” when we want to display the value got from “getchar()”. The last printf statement is used to add the line break at the end of the character value. The program is completed and ready to be compiled.
Ubuntu 20.04 Linux system supports the “GCC” compiler of C language to make our codes error-free before their executions. It’s very easy to install with the “apt” package when you are working within the Ubuntu 20.04 system. So, we have been using this compiler to make our C code error-free as demonstrated in the image instruction. After the C code compilation, the “a.out” instruction has been executed to run the C compiled code. The user added character “A” upon asked and the getchar() function has saved it to the variable “var”. The putchar() function is displaying the input character value added by a user on the shell screen.
Let’s take a look at another example to use the getchar() function in C code. So, within this program, we have taken a main() function to perform the work. The character variable has been declared and the printf statement is here to state that the user has to enter some value. The overall working has been based on the while loop that is here to check the condition i.e., variable value is not equal to “e”. Until the condition is satisfied and the user didn’t enter “e”, the loop will continue taking input from the user as a character and display it on the shell. To get input, the getchar() function has been used, and to display the input got from the user, the “putchar()” function has been used within the while loop. When the user will enter “e”, the loop will end automatically and the program will be completed.
After compiling this code with the gcc compiler, we have got no errors. On execution, the user entered “y” and it got displayed as well. The user entered “b” and it has also been displayed on the shell. When the user entered “e”, it got displayed and the program got terminated.
The above examples were all about using the putchar() function along with the getchar() function to display the input after getting it. While within this example, we will not be using the putchar() function to display the inputted value. So, within the main() function we have declared two integer type variables i.e., var and “I”. The variable “I” is initialized to 0. Another array variable “A” of character type is declared with size 50. After the printf statement, we have been using the “do-while” loop. The program will continue to take input characters using getchar() and save them to the variable “var”. This “var” character value will be assigned to array variable “A” particular index and increment its index i.e., i++. This will continue to add value to “A” until the value entered by the user doesn’t meet the character “x” and the value entered by a user will be displayed using the printf statement as per the “while” part. So within the input value entered by a user, the loop will take all the values before the character “x”. Save the input in variable “A” and display it on the shell.
The user entered the string having the last word start with “x” at first execution i.e., “xo”. Thus, all the characters before “x” got displayed. On the second execution, two “x” got used and all the characters before the first “x” got displayed.
This article contains a brief description of how to make use of the “getchar()” function in the C program while using Ubuntu 20.04 platform. For this, we have used the putchar() function as a companion function for the getchar() function in our examples. This way, we have achieved our goal to get a character as input from the user and display it on the screen.