c - How to check whether an int input is greater than 2147436647 or smaller than -2147483648? -


doing :

int nbr; if (nbr <= -2147483648 || nbr >= 2147483647)     printf("no way !!"); 

does not write no way !! value under lower limit (for example -2147483650) because inputs numbers become positive !!

int exceed range [-2147436647 2147483647] on platforms wider 32-bit int.

#include <limits.h>  int nbr; #if int_min < -2147483648 || int_max >= 2147483647 if (nbr < -2147483648 || nbr > 2147483647)     printf("no way !!"); #endif 

pre c99 platform may need work handle -2147483648.


to detect if string converted long exceed range,

char buf[100]; buf[0] = 0; fgets(buf, sizeof buf, stdin);  errno = 0; char *endptr; long x = strtol(buf, &endtr, 10); if (buf == endptr) puts("no conversion"); else if (errno) puts("out of long range");  // if `long` wider 32-bit ,  [-2147483648 2147483647] range still needed else if (x < -2147483648 || x > 2147483647) puts("no way !!");  else printf("%ld\n", x); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -