Header Ads Widget

Responsive Advertisement

Difference between getch() and getche (). What is file pointer in C?

 DBL AM (ICT)-2019

In C, getch() and getche() are both functions used for reading a single character from the console, but they differ in how they handle input.

  1. getch():
    • Prototype: int getch(void);
    • This function is part of the conio.h library, which is not a standard C library and is not supported by all compilers.
    • getch() reads a single character from the console but does not display it on the screen. The entered character is immediately returned by the function.
#include <conio.h>

int main() {
    char ch;
    ch = getch();  // The character is read without being displayed on the screen
    // Further processing with the character
    return 0;
}

getche():
  • Prototype: int getche(void);
  • Also part of the conio.h library.
  • Similar to getch(), getche() reads a single character from the console, but it echoes the character on the screen as it is entered.

#include <conio.h> int main() { char ch; ch = getche(); // The character is read and displayed on the screen // Further processing with the character return 0; }

Note: The use of conio.h and its functions (getch() and getche()) is not considered good practice for writing portable and standard C code. If you're developing code that needs to be portable across different compilers and platforms, it's recommended to use standard input/output functions like getchar().

As for the "file pointer" in C, it typically refers to a pointer variable used to keep track of the position of the next character to be read or written in a file. In C, the FILE structure is used to represent a file, and a file pointer is a pointer variable of type FILE* that points to this structure. The fopen(), fclose(), fread(), fwrite(), fseek(), and other file-related functions manipulate the file pointer to perform file operations.


Post a Comment

0 Comments