My answer would be "it depends". :)
If isalpha()
and friends from ctype.h
do what you want, then absolutely use them.
But if not...
If you only had two ranges, as in your example snippet, I don't think it looks too messy. If there are more, maybe put the range test in an (inline) function to reduce the number of booleans visible at a time:
if (in_range(val, a1, b1) || in_range(val, a2, b2) || ... )
(Or name it B(n,a,b)
if you feel the need to save screen estate. )
If the ranges might change in run-time, or there are lots of them, put the limits in a struct
and loop through an array of those. If there truly are many, sort the list and do something smart with it, like a binary search over the lower limits (or whatever). But for a small number, I wouldn't bother.
If the total range of allowed values is small (like unsigned chars with values 0..255), but the number of separate "ranges" is large ("all those with prime values"), then make a table (bitmap) of the values, and test against that. Generate the table any way you like. (isalpha()
is probably implemented like this)
unsigned char is_prime[256] = {0, 0, 1, 1, 0, 1, 0, 1,
...};
if (is_prime[val]) { ...