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 want to be able to check for just a regular normal space (from the space-bar) inside of an if
statement.
Does anybody know what is the character for this?
The ASCII value of
Space
is 32. So you can compare your char to the octal value of 32 which is 40 or its hexadecimal value which is 20.if(c == '\40') { ... }
or
if(c == '\x20') { ... }
Any number after the
\
is assumed to be octal, if the character just after\
is notx
, in which case it is considered to be a hexadecimal.The character representation of a Space is simply
' '
.But if you are really looking for all whitespace, then C has a function (actually it's often a macro) for that:
You can read about
isspace
hereIf 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:
make use of isspace function .
sample code:
source : tutorialpoint
There is no particular symbol for whitespace. It is actually a set of some characters which are customarily:
Use
isspace
standard library function fromctype.h
if you want to check for any of these white-spaces.For just a space, use
' '
.No special escape sequence is required: you can just type the space directly:
In ASCII, space is code 32, so you could specify space by
'\x20'
or even32
, but you really shouldn't do that.Aside: the word "whitespace" is a catch all for space, tab, newline, and all of that. When you're referring specifically to the ordinary space character, you shouldn't use the term.
To check a space symbol you can use the following approach
To check a space and/or a tab symbol (standard blank characters) you can use the following approach
To check a white space you can use the following approach