B
Barry Schwarz
I mean checking if the value of a variable is within some permissible
range eg. an int should always be between INT_MIN and INT_MAX range
Then use fgets and strtol or strtoul.
Remove del for email
I mean checking if the value of a variable is within some permissible
range eg. an int should always be between INT_MIN and INT_MAX range
Then use fgets and strtol or strtoul.
There's your answer then...pereges said:what about matherr ? It's avaiable on my implementation but it clearly
says "not standard C"
After conversion, if I check a against SIZE_MAX and SIZE_MIN..probably
that can help.
size_t is an unsigned integer. Unsigned integers all have the same
"MIN": 0.
You *DONT* have to check for < 0. The value cannot be less than 0.
The following types need not to be checked for < 0:
unsigned char
unsigned short
unsigned int
unsigned long
unsigned long long
size_t
uintptr_t
uintmax_t
uint8_t
uint16_t
uint32_t
uint64_t
uint_fast8_t
uint_fast16_t
uint_fast32_t
uint_fast64_t
time_t
clock_t
Maybe there are more unsigned integer types in the standard, I can not
recall.
Please, do not make another post about checking for < 0... Every time
you want to check a type for < 0 look at my post and find whether it
belongs to the list or not.
... Another Cunningham?
size_t is an unsigned integer. Unsigned integers all have the same
"MIN": 0.
You *DONT* have to check for < 0. The value cannot be less than 0.
The following types need not to be checked for < 0:
unsigned char
unsigned short
unsigned int
unsigned long
unsigned long long
size_t
uintptr_t
uintmax_t
uint8_t
uint16_t
uint32_t
uint64_t
uint_fast8_t
uint_fast16_t
uint_fast32_t
uint_fast64_t
time_t
clock_t
Maybe there are more unsigned integer types in the standard, I can not
recall.
Please, do not make another post about checking for < 0... Every time
you want to check a type for < 0 look at my post and find whether it
belongs to the list or not.
... Another Cunningham?
Ok, I'm sorry for my mistake. I forgot about it. Anyway here's my
program and there seems to be some problem :
return EXIT_FAILURE.if (fgets(a_str, 50, stdin) == NULL)
{
fprintf(stderr, "Error while entering the stirng\n");
return (1);
l = strtoul(a_str, &endptr, 0); /* Convert string to long unsigned
first */
strtoul() converts a string to unsigned long.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".
strtoul() converts a string to unsigned long.
unsigned integers CANNOT have negative values.
See all my previous posts telling you this exact thing!
pereges said:Then how to detect unsigned integer underflow ?? or its not possible
with strtoul and it will simply report as "conversion failed"
pereges said:Then how to detect unsigned integer underflow ?? or its not possible
with strtoul and it will simply report as "conversion failed"
The point is that an input of "-36" is as meaningless as one of
"banana" when passed to strtoul. If you want, for some odd reason, to
accept a negative number and then check that it is positive you need
to use strtol, assign the result to a signed long, and compare that.
Seem like a waste of time, but you can do that it you like.
Unsigned types don't underflow or overflow in C. Rather they "wrap
around". If you attempt to store a negative integer in an unsigned
object, what will actually be stored is a positive value determined
according to the properties of modular arithmetic.
<http://en.wikipedia.org/wiki/Modular_arithmetic>
So unsigned integers cannot, by definition, hold "wrong" values. If you
want, you can, in your program, check the numeric strings you operate
upon for the presence of a '-' prefix before attempting to convert them
to unsigned integers.
pereges said:After conversion, if I check a against SIZE_MAX and SIZE_MIN..probably
that can help.
pereges said:Yeah, I noticed that -1 becomes 65535(UINT_MAX). In that case I will
modify my program to :
l = strtoul(a_str, &endptr, 0); /* Convert string to long unsigned
first */
if (errno == ERANGE)
{
if (abs(l) == HUGE_VAL)
{
fprintf(stderr, "OUT OF RANGE OF REPRESENTABLE VALUES\n");
return (1);
}
}
strtoul does not generate HUGE_VAL (that is a double value). Also
this logic looks odd..
ok
pereges said:Yes it does look odd because that would mean conversion of strings
like "0" would also fail.
But this what I found on one reference site
about strtoul :
"
On success, the function returns the converted integral number as a
long int value.
If no valid conversion could be performed, a zero value is returned.
If the correct value is out of the range of representable values,
ULONG_MAX is returned, an the global variable errno is set to ERANGE"
Note the bit where it says "If no valid conversion could be performed
0 is returned. And yeah, instead of HUGE_VAL it should be ULONG_MAX
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.