The Wayback Machine - https://web.archive.org/web/20240912123634/https://www.geeksforgeeks.org/difference-getchar-getch-getc-getche/
Open In App

Difference between getc(), getchar(), getch() and getche()

Last Updated : 22 Jun, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

All of these functions read a character from input and return an integer value. The integer is returned to accommodate a special value used to indicate failure. The value EOF is generally used for this purpose.

getc()

It reads a single character from a given input stream and returns the corresponding integer value (typically ASCII value of read character) on success. It returns EOF on failure.

Syntax

int getc(FILE *stream);

Example

C




// Example for getc() in C
#include <stdio.h>
int main()
{
    printf("%c", getc(stdin));
    return (0);
}


Input: g (press enter key)
Output: g

An Example Application: C program to compare two files and report mismatches

getchar()

The difference between getc() and getchar() is getc() can read from any input stream, but getchar() reads a single input character from standard input. So getchar() is equivalent to getc(stdin).

Syntax

int getchar(void);

Example

C




// Example for getchar() in C
#include <stdio.h>
int main()
{
    printf("%c", getchar());
    return 0;
}


Input: g(press enter key)
Output: g

getch()

Like the above functions, getch() also reads a single character from the keyboard. But it does not use any buffer, so the entered character does not display on the screen and is immediately returned without waiting for the enter key.

getch() is a nonstandard function and is present in <conio.h> header file which is mostly used by MS-DOS compilers like Turbo C. It is not part of the C standard library or ISO C, nor is it defined by POSIX.

Syntax

int getch();

Example

C




// Example for getch() in C
#include <conio.h>
#include <stdio.h>
int main()
{
    printf("%c", getch());
    return 0;
}


Input:  g (Without enter key)
Output: Program terminates immediately.
        But when you use DOS shell in Turbo C,
        it shows a single g, i.e., 'g'

getche()

getche() function reads a single character from the keyboard and displays immediately on the output screen without waiting for enter key. Like getch(), it is also a non-standard function present in <conio.h> header file.

Syntax

int getche(void);

Example

C




// Example for getche() in C
#include <conio.h>
#include <stdio.h>
int main()
{
    printf("%c", getche());
    return 0;
}


Input:  g(without enter key as it is not buffered)
Output: Program terminates immediately.
        But when you use DOS shell in Turbo C, 
        double g, i.e., 'gg'



Similar Reads

What is return type of getchar(), fgetc() and getc() ?
In C, return type of getchar(), fgetc() and getc() is int (not char). So it is recommended to assign the returned values of these functions to an integer type variable. char ch; /* May cause problems */ while ((ch = getchar()) != EOF) { putchar(ch); } Here is a version that uses integer to compare the value of getchar(). int in; while ((in = getcha
1 min read
EOF, getc() and feof() in C
In C/C++, getc() returns the End of File (EOF) when the end of the file is reached. getc() also returns EOF when it fails. So, only comparing the value returned by getc() with EOF is not sufficient to check for the actual end of the file. To solve this problem, C provides feof() which returns a non-zero value only if the end of the file has reached
3 min read
getchar Function in C
C getchar is a standard library function that takes a single input character from standard input. The major difference between getchar and getc is that getc can take input from any no of input streams but getchar can take input from a single standard input stream. It is defined inside the &lt;stdio.h&gt; header file.Just like getchar, there is also
3 min read
Difference and Similarities between PHP and C
PHP is a server-side scripting language designed specifically for web development. It can be easily embedded in HTML files and HTML codes can also be written in a PHP file. The thing that differentiates PHP from a client-side language like HTML is, PHP codes are executed on the server whereas HTML codes are directly rendered on the browser. C is a
3 min read
Difference between ++*p, *p++ and *++p
Predict the output of following C programs. C/C++ Code // PROGRAM 1 #include &lt;stdio.h&gt; int main(void) { int arr[] = {10, 20}; int *p = arr; ++*p; printf(&quot;arr[0] = %d, arr[1] = %d, *p = %d&quot;, arr[0], arr[1], *p); return 0; } C/C++ Code // PROGRAM 2 #include &lt;stdio.h&gt; int main(void) { int arr[] = {10, 20}; int *p = arr; *p++; pri
2 min read
What’s difference between “array” and “&amp;array” for “int array[5]” ?
If someone has defined an array such as “int array[5]”, what’s the meaning of “array” or “&amp;array”? Are they both same or are they different? You might be tempted to think that they both would point to the very first element of the array i.e. they both will have same address. Let us find out! To check this, the very first thing that comes to min
4 min read
Difference between strlen() and sizeof() for string in C
sizeof() Sizeof operator is a compile time unary operator which can be used to compute the size of its operand. The result of sizeof is of unsigned integral type which is usually denoted by size_t.sizeof can be applied to any data-type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as
2 min read
Difference Between Constants and Variables in C
The constants and variables in C are both used to store data. So it is essential to know the difference between the variables and constants in C so that we can decide which one to use based on the situation. In this article, we will discuss the basic difference between a constant and a variable in C language. Variables in C A variable in simple ter
3 min read
Difference between Header file and Library
Header Files: The files that tell the compiler how to call some functionality (without knowing how the functionality actually works) are called header files. They contain the function prototypes. They also contain Data types and constants used with the libraries. We use #include to use these header files in programs. These files end with .h extensi
3 min read
Difference between fundamental data types and derived data types
In computer programming, data type is a classification that specifies to compiler or interpreter which type of data user is intending to use. There are two types of data types - Primitive/Fundamental data type: Each variable in C/C++ has an associated data type. Each data type requires different amounts of memory and has some specific operations wh
8 min read
Difference between Static and Shared libraries
In programming, a library is a collection of pre-compiled pieces of code that can be reused in a program. Libraries simplify life for programmers, in that they provide reusable functions, routines, classes, data structures and so on which they can be reused in the programs. Static Libraries: A Static library or statically-linked library is a set of
2 min read
Difference between Java and C language
Here are some of the differences between Java and C language. C is much faster than Java Java is slower than C due to overhead. C Java C was developed by Dennis M. Ritchie between 1969 and 1973.Java was developed by James Gosling in 1995.C is a Procedural Programming Language.Java is Object-Oriented language.C is more procedure-oriented.Java is mor
3 min read
Difference between C and Python
Here are some of the differences between C and Python. CPythonAn Imperative programming model is basically followed by C.An object-oriented programming model is basically followed by Python.Variables are declared in C.Python has no declaration.C doesn’t have native OOP.Python has OOP which is a part of the language.Pointers are available in C langu
2 min read
Difference between Structure and Array in C
Array in C An array is collection of items stored at contiguous memory locations. Structure in C A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. Difference between Structure and Array ARRAYSTRUCTUREArray refers to a collection consistin
2 min read
Difference between pointer to an array and array of pointers
Pointer to an array: Pointer to an array is also known as array pointer. We are using the pointer to access the components of the array. int a[3] = {3, 4, 5 }; int *ptr = a; We have a pointer ptr that focuses to the 0th component of the array. We can likewise declare a pointer that can point to whole array rather than just a single component of the
4 min read
Difference between scanf() and gets() in C
scanf()It is used to read the input(character, string, numeric data) from the standard input(keyboard).It is used to read the input until it encounters a whitespace, newline or End Of File(EOF). For example see the following code: C/C++ Code // C program to see how scanf() // stops reading input after whitespaces #include &amp;lt;stdio.h&amp;gt; in
3 min read
Difference between C and C#
C language: C language is a middle programming language that was developed at Bell research lab in 1972 by Dennis Ritchie. C language combines the properties of low level and high-level language. therefore its thought-about a middle programming Language. C may be a high-level classical kind programming language that permits you to develop computer
2 min read
Difference between Static variables and Register variables in C
static variables Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope. Syntax: static data_type var_name = var_value; register variables Registers are faster than memory to access
2 min read
Difference between Argument and Parameter in C/C++ with Examples
Argument An argument is referred to the values that are passed within a function when the function is called. These values are generally the source of the function that require the arguments during the process of execution. These values are assigned to the variables in the definition of the function that is called. The type of the values passed in
4 min read
Difference between Type Casting and Type Conversion
1. Type Casting: In typing casting, a data type is converted into another data type by the programmer using the casting operator during the program design. In typing casting, the destination data type may be smaller than the source data type when converting the data type to another data type, that's why it is also called narrowing conversion. Synta
3 min read
Difference between C and Objective C
The language C was developed in early 1970s by Dennis Ritchie for the UNIX Operating system. It a general purpose, procedural programming language. The language is used for developing system applications as well as desktop applications. To learn more about C language refer to https://www. geeksforgeeks. org/c-programming-language/ Objective C was d
4 min read
Difference between Sentinel and Counter Controlled Loop in C
Sentinel Controlled Loop A sentinel controlled loop is also called an indefinite repetition loop because the number of iterations is not known before the loop starts executing. In a sentinel controlled loop, a special value called sentinel value is used to change the loop control expression from true to false in order to determine whether to execut
3 min read
Difference between C and Dart
1. C language : C language is a programming language developed at Bell research lab in 1972 by Dennis Ritchie. C language combines the properties of low level and high-level language. C is a high-level classical kind programming language that permits to develop computer code and movable applications. 2. Dart language : Dart is an object-oriented pr
2 min read
Difference between Identifiers and Variables in C
Perquisites: Identifiers, Variables Identifiers Identifiers are used for the naming of variables, functions, and arrays. It is a string of alphanumeric characters that begins with an alphabet, or an underscore( _ ) that are used for variables, functions, arrays, structures, unions, and so on. It is also known as the user-defined word. Identifier na
2 min read
Difference between Array and Union in C
1. Array in C : An array is collection of similar data items accessed by a common name stored at continuous memory locations. The elements of an array can be accessed using indices. They can be used to store primitive data types such as int, float, double, char, etc. but all elements have to be of the same data type. Below is the picturesque repres
2 min read
Difference between int* p() and int (*p)()?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, a pointer must be declare before storing any variable address. The general form of a pointer variable declaration is: Syntax: type *var_name; Here, type is the pointers base type. It must be a valid C/C
2 min read
Difference between Dangling pointer and Void pointer
Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. There are three different ways where Pointer acts as a dangling pointer: By deallocating memoryFunction CallWhen the variable goes out of scope Void pointer: Void pointer is a specific pointer type – void * – a pointer that point
2 min read
Difference between Struct and Enum in C/C++ with Examples
Structure in C++ A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. The ‘struct’ keyword is used to create a structure. Syntax: struct structureName{ member1; member2; member3; . . . memberN; }; Below is the implementation: C/C++ Code #inc
3 min read
Difference between #include and #include" " in C/C++ with Examples
Pre-requisites: Header files in C/ C++ and its uses The difference between the two types is in the location where the preprocessor searches for the file to be included in the code. #include &lt;filename&gt; // Standard library header #include "filename" // User defined header #include&lt;filename&gt; #include&lt;&gt; is for pre-defined header files
4 min read
Difference between system() and execl() call
A system() call is a way for the programs to interact with the operating system. It is used to execute a command within a process. The system call provides the services of the operating system to the user programs via Application Program Interface (API). It provides an interface between a process and an operating system and it does not replace the
4 min read