Difference Between Getc(), Getchar(), Getch() and Getche()

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

When you use functions like getc(), fgetc(), and getchar() to read characters, they return a special value called EOF if something goes wrong or if there are no more characters to read.

getc()

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

Syntax

int getc(FILE *stream);

Example

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

getchar()

  • getc():
    • Reads a character from any specified input stream.
    • Example: char ch = getc(filePointer);
  • getchar():
    • Reads a single character from the standard input (stdin).
    • Equivalent to getc(stdin).
    • Example: char ch = getchar();

Syntax

int getchar(void);

Example

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

Output

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



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

Output

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:

getche() reads a character from the keyboard and immediately displays it on the screen without waiting for the enter key. It’s a non-standard function in <conio.h>, commonly used in MS-DOS compilers like Turbo C. Example: char ch = getche();

Syntax

int getche(void);

Example



// 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'

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

Q1. What is the difference between Getchar () and gets () function?

Ans. Reads from stdin until an end-of-line or end-of-file.
Unsafe due to lack of buffer size checking.
Prone to buffer overflow vulnerabilities.
getchar():
Reads a single character from stdin.
Safer alternative compared to gets().

Q2. What is the difference between getc and gets?

Ans.getc() and gets() Functions:
getc():
Reads a single character from a file stream.
Used for character-wise reading.
Example: char ch = getc(filePointer);
gets():
Reads a string from the standard input (stdin).
Deprecated and considered unsafe due to lack of buffer size checking.
Differences:
getc() reads characters individually.
gets() reads a sequence of characters until an end-of-line or end-of-file.

Q3. What is the use of Getchar () in C with example?

Ans.Usage:
Used when single-character input is needed from the user.
Operation:
Reads the input as an unsigned char.
Casts and returns it as an int or EOF.
Return Values:
Returns the character as an int or EOF.
EOF is returned if the end of the file is reached or an error occurs.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment