Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

non-sense assertion in VC7.1

0 views
Skip to first unread message

lauch2

unread,
Sep 20, 2005, 9:59:02 PM9/20/05
to
Try to compiler and run the following code in DEBUG mode, an assertion dialog
will be popped. Can I ignore this assertion dialog and why does this code
cause to an assertion?

--
int _tmain(int argc, _TCHAR* argv[])
{
char c = -3;
if(isdigit(c))
{
puts("Character '-3' is a digit");
}
else
{
puts("Character '-3' is not a digit");
}
return 0;
}
--

Thanks in advace
lauch2.

Doug Harrison [MVP]

unread,
Sep 21, 2005, 12:01:10 AM9/21/05
to

The <ctype.h> functions take ints and are defined only on those values of
int that are representable in unsigned char, plus EOF. In VC++ and most
other compilers, plain char is signed, and it's undefined to pass char(-3)
to functions like isdigit, because it gets promoted to int(-3). The
solution is to cast to unsigned char, e.g.

if (isdigit((unsigned char) c)) ...

--
Doug Harrison
VC++ MVP

0 new messages