I am trying to figure out how to check if a character is equal to white-space in C. I know that tabs are \'\\t\'
and newlines are \'\\n\'
, but I wa
The character representation of a Space is simply ' '
.
void foo (const char *s)
{
unsigned char c;
...
if (c == ' ')
...
}
But if you are really looking for all whitespace, then C has a function (actually it's often a macro) for that:
#include
...
void foo (const char *s)
{
char c;
...
if (isspace(c))
...
}
You can read about isspace
here
If you really want to catch all non-printing characters, the function to use is isprint
from the same library. This deals with all of the characters below 0x20 (the ASCII code for a space) and above 0x7E (0x7f is the code for DEL, and everything above that is an extension).
In raw code this is equivalent to:
if (c < ' ' || c >= 0x7f)
// Deal with non-printing characters.