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

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

    It must terminate the string, so yes it will always add '\0' at the end. However, fgets might not always add the newline, if it doesn't fit. I recommend this reference page for fgets.

提交回复
热议问题