-1

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?

ansy
  • 3
  • 3
  • `[Enter]` is a key on a keyboard, not a character. If you mean a newline character for line-based inputs, you can use `fgets`. Never use `gets`. Never. – Cheatah Dec 09 '21 at 07:31

2 Answers2

2

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()

rascadux
  • 56
  • 6
0
#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;
}
Edmund
  • 332
  • 1
  • 9
  • I've tried that. To finish the input by pressing Enter, I need to fill all the 19 characters. But how can I finish, if I need to input for example 10 characters? – ansy Dec 10 '21 at 13:52