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

前端 未结 3 1368
[愿得一人]
[愿得一人] 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 14:03

    According to man page it clearly given :

     char *fgets(char *s, int size, FILE *stream);
    

    fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.

提交回复
热议问题