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?

- 139
- 7

- 553
- 4
- 7
- 12
-
1@sipwiz - while \0x20 is often usable for spaces, there's the issue of different character encodings to worry about. Not all 8-bit encodings are ASCII based, and a byte-stream these days might be an encoded unicode string. \0x20 is correct for UTF-8, of course, but not for some other encodings. These *may* be represented using some other type than a char array, but not necessarily - char arrays as byte streams is such a common pattern for I/O handling irrespective of what the bytes represent. – Jul 21 '10 at 23:48
-
@Steve314 if the OP is using a non-ASCII or non-UTF8 encoding then a good bet is that he'd already know the answer to his question since he'd have already had a few hoops to jump through :). – sipsorcery Jul 22 '10 at 00:06
-
@sipwiz - code gets recycled and moved around. The point is that it's good for code to be portable. Whatever platform it's written for, it may be running on something else later. – Jul 22 '10 at 00:33
-
another trick is using `\x20` for space – Dee Jan 25 '22 at 14:18
5 Answers
You mean "blanks" like in "a b"
? That's a space: ' '
.
Here's a list of escape sequences for reference.

- 494,350
- 52
- 494
- 543
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.

- 233,326
- 40
- 323
- 462
Space is simply ' '
, in hex it is stored as 20, which is the integer equivalent of 32. For example:
if (a == ' ')
Checks for integer 32. Likewise:
if (a == '\n')
Checks for integer 10 since \n
is 0A
in hex, which is the integer 10.
Here are the rest of the most common escape sequences and their hex and integer counterparts:
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

- 73
- 1
- 4
\b
is backspace (ASCII 0x8). You don't need an escape for regular space (ASCII 0x20). You can just use ' '
.

- 278,309
- 50
- 514
- 539
-
1Actually, it could be useful for situations where whitespace is "normalized out", such as in XML fields – glopes Feb 22 '18 at 10:15
'\b' is backspace, and you don't really need an escape sequence for blanks as ' ' will do just fine.

- 1,443
- 4
- 18
- 23