Dev Frank
3 min readDec 20, 2023

--

UNFORMATTED INPUT FUNCTION IN C

@enehfranklyn8

Let’s assume you have a robot friend in C programming. When you ask the robot to listen to what you say and remember it exactly as you say it one letter at a time , that’s like using an unformatted input function. It takes whatever you tell it without changing anything, just like your robot friend does with your words in C!

These are function stored in the header files <STDIO.H> and <CONIO.H>. They are used instead of the formatted input function to take in single character.

getchar()

getchar() is a function in C programming that reads a single character from the standard input stream (usually the keyboard). It waits for the user to input a character and press the Enter key. This function is part of the standard input/output library (stdio.h).

Unlike the formatted input function (scanf), getchar() does not take any argument.

It takes a single character from the user and displays it on the console.

How To Call getchar() Function

You declare a character using the int or char data type, then you initialize it with the getchar() function.

So that any character the getchar() function takes, it’s going to be stored in the variable name.

SYNTAX OF getchar()

N.B: It returns its ASCII value as an integer, when %d or %i is used as the format specifier.

getch()

It is an unformatted input function i.e it does not take any argument.

Unlike getchar(), getch() function takes user input ( character, alphanumeric, symbols, ENTER KEY) but does not display it on the screen. It just goes to the next line of code and execute it. Also part of the <conio.h> library.

Basically getch() function just holds the screen.

It’s used for security purpose like for password, so that they can see what input you are given

SYNTAX OF getch()

N. B: This function is stored in the header file <CONIO. H>.

getche()

getche() function has it similarity with the getch() function. The cursor jumps to the next line without waiting for the user to press the ENTER KEY, unlike getchar() function.

The “e” in the getche() function stands for echoed.

It takes a single character from the user and displays it in the console.

SYNTAX OF getche()

gets()

This unformatted input function takes in sequences of characters i.e strings from the user and displays it in the console.

Remember unformatted input function does not take arguments. It reads characters until a newline character (’\n’) is encountered, and it stores the input as a string.

SYNTAX OF gets()

NB: The use of gets() is not recommended due to the potential for buffer overflow.

It’s worth noting that gets() is considered unsafe because it doesn’t check the size of the input, which can lead to buffer overflow. It’s recommended to use safer alternatives like fgets() for reading strings.

In conclusion, understanding unformatted input functions in C, such as `getchar()`, `getch()`, `getche()`, and `gets()`, provides essential tools for interacting with user input. While these functions serve distinct purposes, it’s crucial to use them judiciously and consider safety aspects, especially in the case of functions like `gets()` which can lead to buffer overflow vulnerabilities. As we continue to explore C programming, incorporating these input functions will enhance our ability to build interactive and user-friendly applications.

Happy coding!

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.