Validate max integer in scanf

前端 未结 4 1732
独厮守ぢ
独厮守ぢ 2020-12-15 11:20

I want to read a int from stdin but I want to validate if the user exceeds the int max value. How can I do it?

int n;
scanf(\"%d\", &n);
<
4条回答
  •  一整个雨季
    2020-12-15 12:07

    Read it into a string and check the length, then call either atol() or sscanf() on the string to convert it into a int.

提交回复
热议问题