fgets adds \0 or \n at the end of the input?

前端 未结 3 1359
[愿得一人]
[愿得一人] 2021-01-03 13:14

I\'ve some doubts about fgets. From what I know, it adds \"\\n\" at the end of the string, and not \"\\0\". So if I write this code:

fgets(buff,2,stdin);
pri         


        
3条回答
  •  温柔的废话
    2021-01-03 13:54

    char * fgets ( char * str, int num, FILE * stream );
    

    Reads characters from input stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

    A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.

    A terminating null character ('\0') is automatically appended after the characters copied to str.

提交回复
热议问题