I've tried different types of string input(scanf, getchar, gets), but none of them can finish input with Enter. Do you guys have any ideas?
CodePudding user response:
As Cheatah said, the function you looking for is fgets()
. Always try to avoid gets()
as it offers no protections against a buffer overflow vulnerability and can cause big problems in your program. You can read some of the answers to this question to clarify the utility of each function
Scanf() vs gets() vs fgets()
CodePudding user response:
#include <stdio.h>
char str[20]; // String with 19 characters (because last character is null character)
int main() {
fgets(str, 20, stdin); // Read string
puts(str); // Print string
return 0;
}