I mean checking if the value of variable is within the permissible range other wise simply report error message and quit. For eg. int is always between INT_MIN and INT_MAX. Is there any elegant way to do this ?
I mean checking if the value of variable is within the permissible range other wise simply report error message and quit. For eg. int is always between INT_MIN and INT_MAX. Is there any elegant way to do this ?
Just compare the value with the endpoints of the range. Of course, an int will be between INT_MIN and INT_MAX anyway.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If the intent of the question is checking if input data fits in an int, then ....
strtol() [for conversion of a string to a long] returns zero if the conversion is not possible. If the data in the string cannot be represented in a long (eg value too big) the function returns LONG_MIN or LONG_MAX (depending on sign) and sets errno to the value of ERANGE.
You might also wish to look up functions like atoi() which, except for behaviour on error, are equivalent to strtol() but simpler to use.
Similar functions exist for converting a string to other types.
Those things do not happen with functions like strtol() -- these functions catch the overflow by parsing the input string, and then return a values and/or assign error status if required.
It would be a fairly oddball parsing technique that could produce a divide by zero error when attempting to interpret contents of a string as an integral value.
No, grumpy stated that "those things do not happen with functions like strtol()", which is true.So you are saying that using strtol can cause divide by zero error ?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ok . What about when reading double precision data from a file ? Is it safer to use fgets or fscanf ? The file format is as follows:
3.456789 0.000000 -1.234567
4.567889 0.000001 -10.22222
..................................................
..................................................
If I use fgets, I presume I will be reading 3 strings and then converting each to double precision using strtol ? But I think fgets only allows one string at a time.
You may want to read up on strtol
http://cboard.cprogramming.com/showthread.php?t=104680
The intended purpose is for conversion. I would not base my calculations for boundary checking on this.
I would recommend to use assert() if at all possible if you want to check for a condition and exit. If you are debugging.
http://www.cprogramming.com/tips/sho...ount=30&page=0
Hello, I looked up the strto* family of functions and I applied it my problem of scanning a size_t variable. Here's my program and something seems to be going wrong :
The program gives proper output until you enter a negative number forCode:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <math.h> #include <limits.h> int main(void) { size_t a; /* size_t variable */ char a_str[50]; /* String to be converted */ char *ptr = NULL; char *endptr; unsigned long l; if (fgets(a_str, 50, stdin) == NULL) { fprintf(stderr, "Error while entering the stirng\n"); return (1); } if ((ptr = strchr(a_str, '\n')) != NULL) { *ptr = '\0'; } errno = 0; l = strtoul(a_str, &endptr, 0); /* Convert string to long unsigned first */ if (errno == ERANGE) { if (abs(l) == HUGE_VAL) { fprintf(stderr, "OVERFLOW\n"); return (1); } else if (l == 0) { fprintf(stderr, "UNDERFLOW\n"); return (1); } } else { if (l == 0.0) { fprintf(stderr, "Conversion failed\n"); return (1); } } if (l <= UINT_MAX) /* size_t typedefed as uint on my tcc */ { a = (size_t)l; printf("\n%lu", (unsigned long)a); } else { printf("OVERFLOW"); } return (0); }
which, ideally, it should print "UNDERFLOW" but it seems to print
"Conversion failed".
strtoul() doesn't even try to convert numbers that begin with "-". It sees that they'll always fail. So it just returns immediately, I guess, without setting errno.The program gives proper output until you enter a negative number for
which, ideally, it should print "UNDERFLOW" but it seems to print
"Conversion failed".
If you want to tell if it was a negative number, you could use something like this . . . .
[edit] By the way:Code:int is_negative(const char *string) { while(isspace(*string)) string ++; return *string == '-'; }
This is a bit easier to maintain:Code:if (fgets(a_str, 50, stdin) == NULL)
[/edit]Code:if (fgets(a_str, sizeof(a_str), stdin) == NULL)
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.