문제

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

도움이 되었습니까?

해결책

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.

다른 팁

I've never really understood their differences

Those methods have clearly different signatures (that resemble the semantics quite obviously IMHO):

 char *gets( char *str );

 int getch(void);

Note that getch() is platform specific notation, the equivalent standard defined getc() function wants a FILE* parameter.

The getch is a platform or compiler specific function.

The gets function is used for returning zero or more characters from the standard (console) input.

Edit 1:
Depending on your compiler implementation, the getch function may be used to get a character without waiting for the user or to get a character without echoing to the console.

One function gets single character, the other gets many.

gets() has been removed in C11 and marked as deprecated in C++11.

I've not heard of getch(). Do you mean getchar()?

In any case, you should be able to look up documentation yourself. Those two functions are part of cstdio

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top