Search code examples
c++getsgetch

What is the difference between gets() and getch()?


I've heard of both function but I've never really understood their differences. Is gets() for getting a string without pressing enter and getch() for getting only one character without pressing enter?

Thanks


Solution

  • getch() is a function in conio.h on windows and DOS systems that's not standardized in C. It's used to get only (exactly) a single character. It also exists in curses.h on *nix systems (according to Mike in the comments).

    gets() is a standard function in C, which got removed in the new C11 standard. You can use it to input a string, but you should never use it. You can't use it to safely input a string, because it writes to memory as long as you type. So you can get buffer overflows quite easily.