C语言 C中空格的符号是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30033582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:54:51  来源:igfitidea点击:

What is the symbol for whitespace in C?

cwhitespacespace

提问by Cesar A

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 ifstatement.

我想弄清楚如何检查一个字符是否等于 C 中的空格。我知道制表符是'\t'和换行符是'\n',但我希望能够检查一个常规的正常空间(从空格键)if语句中。

Does anybody know what is the character for this?

有谁知道这个角色是什么?

回答by haccks

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

空白没有特定的符号。它实际上是一组一些习惯上的字符:

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

Use isspacestandard library function from ctype.hif you want to check for any of these white-spaces.

如果要检查这些空格中的任何一个,请使用isspace标准库函数ctype.h

For just a space, use ' '.

对于一个空间,使用' '.

回答by kdopen

The character representation of a Space is simply ' '.

Space 的字符表示很简单' '

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:

但是如果你真的在寻找所有的空白,那么 C 有一个函数(实际上它通常是一个宏):

#include <ctype.h>
...

void foo (const char *s)
{
    char c;
    ...
    if (isspace(c))
        ...
}

You can read about isspacehere

你可以在这里阅读isspace

If you really want to catch all non-printing characters, the function to use is isprintfrom 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).

如果您真的想捕获所有非打印字符,则要使用的函数isprint来自同一个库。这将处理 0x20(空格的 ASCII 代码)以下和 0x7E(0x7f 是 DEL 的代码,其上的所有内容都是扩展名)以上的所有字符。

In raw code this is equivalent to:

在原始代码中,这等效于:

if (c < ' ' || c >= 0x7f)
    // Deal with non-printing characters.

回答by Sujit Menon

The ASCII value of Spaceis 32. So you can compare your char to the octal value of 32 which is 40 or its hexadecimal value which is 20.

的 ASCII 值Space是 32。因此,您可以将 char 与 32 的八进制值 40 或其十六进制值 20 进行比较。

if(c == '\40') { ... }

if(c == '\40') { ... }

or

或者

if(c == '\x20') { ... }

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.

之后的任何数字都\被假定为八进制,如果紧随其后的字符\不是x,在这种情况下,它被认为是十六进制。

回答by Sujit Menon

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 couldspecify space by '\x20'or even 32, but you really shouldn't do that.

在 ASCII 中,空格是代码 32,因此您可以通过'\x20'甚至指定空格32,但您真的不应该这样做。

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.

旁白:“空白”这个词是空格、制表符、换行符和所有这些的统称。当您特指普通空格字符时,不应使用该术语。

回答by Tirupati Rao

make use of isspace function .

利用 isspace 功能。

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

C 库函数 int isspace(int c) 检查传递的字符是否为空白字符。

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

来源:教程点

回答by Vlad from Moscow

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( c ) ) { /*...*/ }

To check a white space you can use the following approach

要检查空白,您可以使用以下方法

#include <ctype.h>

//...

if ( isspace( c ) ) { /*...*/ }

回答by stpd_rssn

#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;
  }
 }
}