50

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?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Cesar A
  • 663
  • 1
  • 7
  • 10

7 Answers7

82

There is no particular symbol for whitespace. It is actually a set of few characters which are:

' '      space 
'\t'     horizontal tab 
'\n'     newline
'\v'     vertical tab 
'\f'     form feed 
'\r'     carriage return    

Use isspace standard library function from ctype.h if you want to check for any of these white-spaces.

For just a space, use ' '.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 3
    `isspace` would return affirmative for `'\t'`, which the OP doesn't want. –  May 04 '15 at 15:12
  • 1
    Assuming he wants to check for all whitespace characters, and not just space. – Thomas Padron-McCarthy May 04 '15 at 15:13
  • 6
    @Hurkyl The title of the question says *whitespace*. Maybe it is time to educate the OP about the difference between the space character, and the set of characters that are considered to be whitespace. – Sinan Ünür May 04 '15 at 15:17
13

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 <ctype.h>
...

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.
kdopen
  • 8,032
  • 7
  • 44
  • 52
8

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 not x, in which case it is considered to be a hexadecimal.

Sujit Menon
  • 89
  • 1
  • 2
  • 2
    `'\40'`, `'\x20'`, `32`, `' '` are all absolutely the same in an ASCII based C implementation. The same type (`int`). The same value. The same internal representation. The same (if any) padding. The same (if any) trap representations. Of all 4, only the last one is guaranteed to represent a space on every computer with a C implementation, whether it's ASCII-based, or EBCDIC-based, or KLINGON-based. – pmg Aug 26 '18 at 18:18
  • Do the \40 or \x20 or 32 need to be inside single quotes? I mean, single quotes are used for characters, and to me those three aren't really characters. They're integers. What am I missing here? – Anurag Baundwal Dec 19 '19 at 17:46
4

No special escape sequence is required: you can just type the space directly:

if (char_i_want_to_test == ' ') { 
    // Do something because it is space
}

In ASCII, space is code 32, so you could specify space by '\x20' or even 32, 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.

4

To check a space symbol you can use the following approach

if ( c == ' ' ) { /*...*/ }

To check a space and/or a tab symbol (standard blank characters) you can use the following approach

#include <ctype.h>

//...

if ( isblank( ( unsigned char )c ) ) { /*...*/ }

To check a white space you can use the following approach

#include <ctype.h>

//...

if ( isspace( ( unsigned char )c ) ) { /*...*/ }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

make use of isspace function .

The C library function int isspace(int c) checks whether the passed character is white-space.

sample code:

    int main()
    {

       char var= ' ';

       if( isspace(var) )
       {
          printf("var1 = |%c| is a white-space character\n", var );
       }
/*instead you can easily compare character with ' '  
  */     
    }
Standard white-space characters are −

' '   (0x20)    space (SPC)
'\t'    (0x09)  horizontal tab (TAB)
'\n'    (0x0a)  newline (LF)
'\v'    (0x0b)  vertical tab (VT)
'\f'    (0x0c)  feed (FF)
'\r'    (0x0d)  carriage return (CR)

source : tutorialpoint

Tirupati Rao
  • 615
  • 6
  • 24
-2
#include <stdio.h>
main()
{
int c,sp,tb,nl;
sp = 0;
tb = 0;
nl = 0;
while((c = getchar()) != EOF)
{
   switch( c )
{
   case ' ':
        ++sp;
     printf("space:%d\n", sp);
     break;
   case  '\t':
        ++tb;
     printf("tab:%d\n", tb);
     break;
   case '\n':
        ++nl;
     printf("new line:%d\n", nl);
     break;
  }
 }
}