
Untitled
By: a guest on
Apr 26th, 2012 | syntax:
None | size: 0.70 KB | hits: 15 | expires: Never
C - Check that an int read by scanf is in the range of the integers
int e_1;
char c[1];
// noNeedToCleanBuffer is used to avoid buffer cleaning the first time.
int noNeedToCleanBuffer = 1;
do {
// Clears the buffer.
if (!noNeedToCleanBuffer)
while ((c[0] = getchar()) != 'n') ;
noNeedToCleanBuffer = 0;
printf("Input an integer value: n");
e_1 = scanf("%d", &n);
c[0] = getchar();
} while ((e_1 != 1 && c[0] != 10) || (e_1 == 1 && c[0] != 10));
num = INT_MAX;
i = 0;
while(num != 0){
str[i] = num % 10;
num = num / 10;
i++;
}
int foo = /* some value to test */;
long bar = (long)foo;
if ( (bar < (long)INT_MIN) || (bar > (long)INT_MAX) ) return false;