What is the symbol for whitespace in C?

后端 未结 7 2069
醉话见心
醉话见心 2020-12-13 08: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 wa

7条回答
  •  时光说笑
    2020-12-13 09:52

    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.
    

提交回复
热议问题