why is the backslash-space combo a escape sequence 040

V

vlsidesign

The printf function returns "warning: unknown escape sequence: \040"
for a backslash-space combination. If the ascii decimal number for
space is 32 and the backslash is 92, why this particular number 040?
Is it a decimal number from the ASCII code chart? (compiling using gcc
on SunOS 5.8, Sparc, Ultra-80)
Just curious. Thanks.
 
W

Walter Roberson

The printf function returns "warning: unknown escape sequence: \040"
for a backslash-space combination. If the ascii decimal number for
space is 32 and the backslash is 92, why this particular number 040?
Is it a decimal number from the ASCII code chart?

It is an octal number from the ASCII code chart. 4*8^1 + 0*8^0 = 32 (decimal)
 
K

Keith Thompson

vlsidesign said:
The printf function returns "warning: unknown escape sequence: \040"
for a backslash-space combination. If the ascii decimal number for
space is 32 and the backslash is 92, why this particular number 040?
Is it a decimal number from the ASCII code chart? (compiling using gcc
on SunOS 5.8, Sparc, Ultra-80)

I doubt that the printf function itself is returning this message; the
only escape sequences printf knows about are the ones introduced by
the '%' character. Probably you're using "\040" in a string literal,
and your compiler is complaining about it; the fact that you're using
that string literal in a printf call is irrelevant. Try using the
string literal in another context, and you should get the same message:

char *s = "your literal";

In a string or character literal, the sequence \040 denotes the octal
value of the character (your C textbook should explain this). 040
octal is 32 decimal, which happens to be the ASCII code for the space
character (though C doesn't require ASCII).

It would have been very helpful if you had posted the actual
copy-and-pasted code that caused the problem.

Here's a small C translation unit:

char *backslash_space = "\ ";
char *backslash_bang = "\!";

and gcc's output:

c.c:1:25: warning: unknown escape sequence: '\040'
c.c:2:25: warning: unknown escape sequence '\!'

Presumably gcc shows the space character as 040 to avoid the visual
ambiguity of printing the actual space character.

The C standard doesn't specify the form of diagnostic messages;
compilers other than gcc will produce different messages. For
example, here's the output of another compiler:

"c.c", line 1: warning: dubious escape: \
"c.c", line 2: warning: dubious escape: \!

and another:

"c.c", line 1.25: 1506-235 (W) Incorrect escape sequence \ . \ ignored.
"c.c", line 2.25: 1506-235 (W) Incorrect escape sequence \!. \ ignored.
 
A

Army1987

The printf function returns "warning: unknown escape sequence: \040"
for a backslash-space combination. If the ascii decimal number for
space is 32 and the backslash is 92, why this particular number 040?
Is it a decimal number from the ASCII code chart?
040 is octal for 32.
Backslashes in string literals are always taken as escape
sequences. If you want to use one, try "\\".
 
C

CBFalconer

vlsidesign said:
The printf function returns "warning: unknown escape sequence:
\040" for a backslash-space combination. If the ascii decimal
number for space is 32 and the backslash is 92, why this
particular number 040? Is it a decimal number from the ASCII code
chart? (compiling using gcc on SunOS 5.8, Sparc, Ultra-80)

It's the octal code for a space, as indicated by the leading 0.
The 'escape space' combination is only valid at the termination of
a C source line, and is effectively absorbed.
 
K

Keith Thompson

CBFalconer said:
It's the octal code for a space, as indicated by the leading 0.
The 'escape space' combination is only valid at the termination of
a C source line, and is effectively absorbed.

No, a backslash followed by a space is not legal at the end of a line.
A backslash can be used to join two logical lines but the backslash
must *immediately* precede the new-line character. See C99 5.1.1.2p2.

Some compilers may allow spaces after a backslash as an extension.
It's a reasonable one, since trailing whitespace is typically
invisible. Variations in text file formats may also affect this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top