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);
<
The only way to convert a string representation of a number to the actual value and to watch for overflow is to use functions from strto..
group. In your case you need to read in a string representation of the number and then convert it using strtol
function.
Beware of responses that suggest using atoi
or sscanf
to perform the final conversion. None of these functions protect from overflow.