lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

uint8_t holding '\8'

Mon Sep 29, 2014 6:46 am

uint8_t A holding '\8' is that a string?

however, int result=10+A=18 for result.

why is '\8' of uint8_t type equals to integer 8?

Hugh-Roger
Posts: 27
Joined: Sat Jan 18, 2014 12:31 am

Re: uint8_t holding '\8'

Mon Sep 29, 2014 7:55 am

'\8' is ASCII 8.

User avatar
PeterO
Posts: 4867
Joined: Sun Jul 22, 2012 4:14 pm

Re: uint8_t holding '\8'

Mon Sep 29, 2014 8:15 am

Hugh-Roger wrote:'\8' is ASCII 8.
Not according to gcc it isn't !

Code: Select all

warning: unknown escape sequence: '\8' [enabled by default]
  A = '\8';
       ^
And if it was "ASCII 8" the result of "A + 10" would be 56 + 10 = 66


PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
joan
Posts: 14073
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: uint8_t holding '\8'

Mon Sep 29, 2014 8:18 am

PeterO wrote:
Hugh-Roger wrote:'\8' is ASCII 8.
Not according to gcc it isn't !

Code: Select all

warning: unknown escape sequence: '\8' [enabled by default]
  A = '\8';
       ^
And if it was "ASCII 8" the result of "A + 10" would be 56 + 10 = 66


PeterO
But in that case an executable will be created and A will be assigned the value decimal 56.

SteveSpencer
Posts: 351
Joined: Thu Mar 28, 2013 9:19 am
Location: Nottingham, UK

Re: uint8_t holding '\8'

Mon Sep 29, 2014 8:26 am

Usually, the \nnn notation is used for octal (base 8) values. So gcc will complain because 8 is not a valid octal digit.
\010 would be OK, though, but memory fails me as to whether \10 would be accepted, as I can't remember whether or not a leading zero is required.
Steve S
No, I can't think of anything funny that won't offend someone if they want it to...

User avatar
PeterO
Posts: 4867
Joined: Sun Jul 22, 2012 4:14 pm

Re: uint8_t holding '\8'

Mon Sep 29, 2014 8:53 am

joan wrote: But in that case an executable will be created and A will be assigned the value decimal 56.
Indeed , but the problem is that '\7' and \'8' produce thier results by different interpretations.

'\7' is a valid octal encoded character constant.
'\8' is (as gcc pointed out) an invalid escape sequence, which seems to result in the backslash being ignored and the remaining "8" being used as the single character constant. A clear case where ignoring a compiler warning will get you into trouble !

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 35517
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: uint8_t holding '\8'

Mon Sep 29, 2014 11:06 am

Hugh-Roger wrote:'\8' is ASCII 8.
No it isn't, it's an invalid octal value.

ASCII 8 is backspace (BS) == \10 == 0x8
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
PeterO
Posts: 4867
Joined: Sun Jul 22, 2012 4:14 pm

Re: uint8_t holding '\8'

Mon Sep 29, 2014 11:50 am

DougieLawson wrote:
Hugh-Roger wrote:'\8' is ASCII 8.
No it isn't, it's an invalid octal value.

ASCII 8 is backspace (BS) == \10 == 0x8
No it isn't, it's an invalid escape sequence.

The digit "8" is in a position where an octal number would be valid syntax.

<pedant mode on> How do you define "invalid octal number" ? <pedant mode off>

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 35517
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: uint8_t holding '\8'

Mon Sep 29, 2014 12:01 pm

It looks like we're discussing the pedantry of the compiler. If it checks octal first then checks for escaped strings when it fails on the octal match we get the effect seen above.

If it did it the other way round and checked for octal after checking escapes it would also fail.

It's wrong both ways.
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
PeterO
Posts: 4867
Joined: Sun Jul 22, 2012 4:14 pm

Re: uint8_t holding '\8'

Mon Sep 29, 2014 12:24 pm

The compiler is being accurate, I'm being pedantic......

An octal number consists of only the digits 0,1,2,3,4,5,6,7
Anything with other digits or characters in it is therefore not an octal number.

For the term "invalid octal number" to make sense you need some other critria that can be applied to an octal number to decide if it is valid or not.

e.g. "numbers in the range 0 to 255 are valid in this field".
If I put "01000" in that field is is correct to report "You entered an invalid octal number"
If I put "0A000" it is worng to report an invalid octal number.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
topguy
Posts: 5611
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: uint8_t holding '\8'

Tue Sep 30, 2014 11:26 am

lilzz wrote:uint8_t A holding '\8' is that a string?
however, int result=10+A=18 for result.
why is '\8' of uint8_t type equals to integer 8?
Where did you see '\8' ? In a debugger, in the source ?

Its not a string. "q" is a string, but 'q' is a single character.

The character '8' has the value of 0x38 = 56 in the ascii table, so '\8' should interpreted as the value 8 and not the character '8'.

My guess is that this is an attempt to show the content of A but since 8 is not a printable character, it shows it as '\8' to differentiate from the character '8'.

User avatar
Redrobes
Posts: 80
Joined: Mon Dec 26, 2011 9:19 pm
Location: S.W. UK
Contact: Website

Re: uint8_t holding '\8'

Tue Sep 30, 2014 7:45 pm

See the latest (free) working draft:
https://isocpp.org/files/papers/N3690.pdf

page 26 -> "Escape sequences in which the character following the backslash is not listed in Table 7 are conditionally-supported, with implementation-defined semantics."

I.e. It's an undefined escape sequence so all bets are off so don't do it and GCC can do pretty much whatever it wants with it and that's not incorrect.

Oh yes, to answer the question. No its not a string. And if you add a number to it then any result is an option.

User avatar
Redrobes
Posts: 80
Joined: Mon Dec 26, 2011 9:19 pm
Location: S.W. UK
Contact: Website

Re: uint8_t holding '\8'

Thu Oct 16, 2014 10:40 pm

Relevant post:
http://developerblog.redhat.com/2014/10 ... zer-ubsan/

Not sure that this specific input is something its designed to catch but maybe in the future we will say the solution to your issue is to turn on the undefined behaviour sanitizer.

Return to “C/C++”