Difference Between getc and getchar

A function is a set of statements to perform a specific task. In programming, the user can define his functions or use the functions provided by the programming language. C language has number of functions, so the programmer can use them directly in coding without implementing them from the beginning. There are few functions which associate with character reading. Two of them are getc and getchar. The key difference between getc and getchar is that the getc is used to read a  character from an input stream such as a file or standard input while getchar is to read a character from standard input. This article discusses the difference between these two functions.

CONTENTS

1. Overview and Key Difference
2. What is getc
3. What is getchar
4. Similarities Between getc and getchar
5. Side by Side Comparison – getc vs getchar in Tabular Form
6. Summary

What is getc?

It is a function, used to read a character from an input stream such as a file or a keyboard. It returns the corresponding integer value on success. The syntax of getc is, int getc(File *stream). Refer below example. Assume that test.txt is a plain text file in the project directory. This file has two characters which are ‘a’ and ‘b’.

Figure 01: Reading Characters of a File using getc

According to the above program, the test file is opened in the read mode. Then the first character is read using getc function and stores into variable c1. The printf statement output c1. Then the second character is read and stored in variable c2. The printf statement output c2. Therefore, getc function is used to read a character from a stream such as a file.

Figure 02: Reading Characters in a File Using getc and loop.

It can be used with a loop to read all characters, one at a time till it reaches the End of File (EOF) as above. The two characters in the test.txt file are displayed on the screen.

What is getchar()?

getchar() is used to read a character only from the standard input. It waits till the enter key is pressed and the reading can be seen on the screen. Its syntax is similar to int getchar(void);

The getchar function does not require an argument like getc. By default, getchar works for standard input. Therefore, it is not necessary to pass any argument to getchar function. Refer the below example.

Figure 03: getchar

When the user gives the input character, it is displayed on the screen and waits till the enter key is pressed. After enter key, the output is printed on the screen because of the printf function.

The same getchar functionality can be achieved using getc function as follows.

Figure 04: getchar Functionality Using getc

According to the above program, the getc function is used to read a character. It is stored into “ch” variable. The getc function argues stdin to indicate that the input is taken from the standard input which is the keyboard. The user can give a character and press enter key. Then that character prints to the screen using printf function.

What are the Similarities Between getc and getchar?

  • Both are functions provided by C programming language.
  • Both functions return an End of File (EOF) when the stream ends.

What is the Difference Between getc and getchar?

getc vs getchar

getc is a C function to read a character from an input stream such as a file stream or standard input. getchar is a C function to read a character only from the standard input stream(stdin) which is the keyboard.
 Syntax
getc syntax is similar to int getc(File *stream). getchar syntax is similar to int getchar(void);

Summary – getc vs getchar

The C programming language provides many functions. Programmers can use these functions without implementing them from the beginning. Two of such functions are getc and getchar. The difference between getc and getchar is that getc is used to read a character from an input stream such as a file or standard input and getchar is to read a character from standard input. Both of them are using to read a character, but their functionalities are different.

Download the PDF Version of getc vs getchar

You can download PDF version of this article and use it for offline purposes as per citation note. Please download PDF version here Difference Between getc and getchar

Reference:

1.Ecworks, ‘Difference between gets(),getch(),getche(),getchar()-c program tutorial’,Youtube, 15 Nov. 2016. Available here 
2.VTGAMES,’C Programming, File operation, Character Input (fopen, fclose, getc)’,YouTube, 19 Mar. 2016. Available here
3.tutorialspoint.com. “C library function – getc().”  The Point, Available here