How fget works?

后端 未结 4 1929
傲寒
傲寒 2021-01-23 15:31

I am using gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
I am writing a very simple script to take string as input and print the same with the some custom message. Fi

4条回答
  •  闹比i
    闹比i (楼主)
    2021-01-23 16:08

    This line

    scanf("%d",&T);
    

    reads the input until the first non-numeral is found, which is newline. Then fgets() reads that newline for its first input.

    I suggest using fgets() to read the number too.

    fgets(str,80,stdin);
    sscanf(str, "%d", &T);
    

提交回复
热议问题