Getchar() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The getchar in C is a non-standard function whose definition is already defined in the stdio.h header file and it is used to take the input of a single character(unsigned char) from the standard input(stdin).

The getchar function in C is similar to the getc() function in C with little difference. The getc() function can take the input of a single character from any stream. Whereas getchar in C can take input of a single character only from the standard input stream.

Also, the getch function in C is a non-standard function typically defined in the <conio.h> header file and is commonly used in Windows environments for character input.

Syntax of getchar() in C

  • Following code snippet shows the syntax of getchar() function in C.

Return Values of getchar in C

The getchar in C returns the unsigned char casted to int value of the character it reads on successful execution, otherwise, it returns the EOF at end of the file or in case of error.

Examples of getchar() in C

1. Read Single Character Using getchar in C

See the following code example that shows the implementation of getchar in C to take a single character as an input.

Code Example -

Output -

In the above code snippet, we first declared the variable character of char datatype which is of 1 byte. We took the input from stdin using the getchar in C as shown on line number 11. In this example to show the output of entered character we used the putchar() function in C which is a part of stdio and used to output the single character.

2. Read String of n Characters Using getchar in C

Following is a coding example to take the input of the string having n characters using the getchar in C. Code Example -

Output -

In the above coding example, we first declared the array of n characters to store the string. Here the while loop is used to take the input of characters until we reach the End of File and the user does not press the enter(/n). The ptr is getting incremented after each character insertion to assign the characters sequentially in the buffer array.

3. Implementing Putchar

Here's a C program that demonstrates the implementation of putchar to print the character entered by the user:

Input:

Output:

In this program, the user inputs a character, which is then printed using the custom printChar function implemented with putchar. This demonstrates the functionality of putchar in printing individual characters.

4: Reading Sentences using getchar() Function and do-while Loop

Here's a C program that reads sentences using getchar() and a do-while loop:

Input:

Output:

In this program, the user can enter a sentence, which is read character by character using getchar in C within a do-while loop. The loop continues until the newline character ('\n') is encountered, and the entered sentence is stored in the sentence array. Finally, the entered sentence is printed.

Conclusion

  1. getchar Function in C: In this article we have seen what is getchar in C. It is used to fetch a single character from standard input (stdin) in C programming.
  2. Syntax: int getchar(void).
  3. Return Values: Returns the unsigned char value of the character read, or EOF on error or end-of-file.
  4. Examples: Demonstrated usage includes reading single characters, strings, and implementing putchar functionality.
  5. Usage: Essential for character input handling, especially in interactive command-line interfaces.
  6. Importance: Understanding getchar function in C is crucial for efficient character input management in C programs.