You're joking, right? You've already guarinteed that all values will fall into an unsigned char's range. Now you're trying to say "See, my cast works!". Of course it works! You just forced it to!
Code:
int main(void)
{
char buffer[1024];
int i, j;
for ( i = 0; i < 1024; ++i )
{
int c = getchar();
if ( c == EOF )
{
break;
}
if ( c >= 0 && c <= UCHAR_MAX )
{
buffer[i] = c;
}
}
for ( j = 0; j < i; ++j )
{
#if 1 /* avoid UB */
if ( myisprint((unsigned char)buffer[j]) )
Duh! You've just done what I've told you to do: Validate your data, and the cast is pointless.
The only way it will ever not work is if your char is signed. In which case, you're still doing it wrong. Actually, you've already been "kind of" doing it right. You're just not fully doing the check. You're actually trying to make it look like you're doing the check correctly, when you're intentionally doing it wrong to prove your point. Because if you're going to complain about values less than zero, you should be checking for values above UCHAR_MAX as well. But in the end, your 'myisprint' is very close to what I've been saying all along: Check your work, and then pass it. Don't half ass it, do it right.
Code:
int davesisprint( int c )
{
if( c == EOF || ( c >= 0 && c <= UCHAR_MAX ) )
return isprint( c );
return 0;
}
Now why again do I need a cast here Dave? Oh, that's right, I don't.
Prelude's commenting on us arguing "pointless" semantics? Pot meet kettle?
Quzah.