C语言 空格的 C 转义序列是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3304708/
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 05:58:20  来源:igfitidea点击:

What's the C escape sequence for blanks?

cescaping

提问by MW2000

I'm writing a program to count blanks, tabs, and newlines. I remember what the escape sequence for tabs and newlines are, but what about blanks? \b? Or is that backspace?

我正在编写一个程序来计算空格、制表符和换行符。我记得制表符和换行符的转义序列是什么,但是空格呢?\b? 还是那个退格?

回答by GManNickG

You mean "blanks" like in "a b"? That's a space: ' '.

您的意思是“空白”,例如"a b"?那是一个空格:' '

Here's a list of escape sequencesfor reference.

这里有一个转义序列列表供参考。

回答by caf

If you want to check if a character is whitespace, you can use the isspace()function from <ctype.h>. In the default C locale, it checks for space, tab, form feed, newline, carriage return and vertical tab.

如果要检查字符是否为空格,可以使用isspace()来自<ctype.h>. 在默认的 C 语言环境中,它检查空格、制表符、换页、换行符、回车和垂直制表符。

回答by Elf Machine

Space is simply ' ', in hex it is stored as 20, which is the integer equivalent of 32. For example:

空格很简单' ',在十六进制中它存储为 20,这是 32 的整数等价物。例如:

if (a == ' ')

Checks for integer 32. Likewise:

检查整数 32。同样:

if (a == '\n')

Checks for integer 10 since \nis 0Ain hex, which is the integer 10. Here are the rest of the most common escape sequences and their hex and integer counterparts:

检查整数 10,因为它\n0A十六进制的,也就是整数 10。以下是其余最常见的转义序列及其十六进制和整数对应物:

code: │   name:                │Hex to integer:
──────│────────────────────────│──────────────
\n    │  # Newline             │  Hex 0A = 10
\t    │  # Horizontal Tab      │  Hex 09 = 9
\v    │  # Vertical Tab        │  Hex 0B = 11
\b    │  # Backspace           │  Hex 08 = 8
\r    │  # Carriage Return     │  Hex 0D = 13
\f    │  # Form feed           │  Hex 0C = 12
\a    │  # Audible Alert (bell)│  Hex 07 = 7
\    │  # Backslash           │  Hex 5C = 92
\?    │  # Question mark       │  Hex 3F = 63
\'    │  # Single quote        │  Hex 27 = 39
\"    │  # Double quote        │  Hex 22 = 34
' '   │  # Space/Blank         │  Hex 20 = 32

回答by Matthew Flaschen

\bis backspace (ASCII 0x8). You don't need an escape for regular space (ASCII 0x20). You can just use ' '.

\b是退格(ASCII 0x8)。您不需要对常规空间(ASCII 0x20)进行转义。你可以只使用' '.

回答by Joseph Paterson

'\b' is backspace, and you don't really need an escape sequence for blanks as ' ' will do just fine.

'\b' 是退格键,你真的不需要空格的转义序列,因为 ' ' 就可以了。