user1371943
user1371943

Reputation:

Escape sequence discrepancy

 #include<stdio.h>
 int main()
 {
          int i=0;
          printf("%d:%4d\n",++i,'\1');
          printf("%d:%4d\n",++i,'\2');
          printf("%d:%4d\n",++i,'\3');
          printf("%d:%4d\n",++i,'\4');
          printf("%d:%4d\n",++i,'\5');
          printf("%d:%4d\n",++i,'\6');
          printf("%d:%4d\n",++i,'\7');
          printf("%d:%4d\n",++i,'\8');
          printf("%d:%4d\n",++i,'\9');
          printf("%d:%4d\n",++i,'\10');
          printf("%d:%4d\n",++i,'\11');
          printf("%d:%4d\n",++i,'\12');
          printf("%d:%4d\n",++i,'\13');
          printf("%d:%4d\n",++i,'\14');
          printf("%d:%4d\n",++i,'\15');
          printf("%d:%4d\n",++i,'\16');
          printf("%d:%4d\n",++i,'\17');
          printf("%d:%4d\n",++i,'\18');
          printf("%d:%4d\n",++i,'\19');
          printf("%d:%4d\n",++i,'\20');
          printf("%d:%4d\n",++i,'\21');
          printf("%d:%4d\n",++i,'\22');
          printf("%d:%4d\n",++i,'\23');
          printf("%d:%4d\n",++i,'\24');
          printf("%d:%4d\n",++i,'\25');
          printf("%d:%4d\n",++i,'\26');
          printf("%d:%4d\n",++i,'\27');
          printf("%d:%4d\n",++i,'\28');
          printf("%d:%4d\n",++i,'\29');
          printf("%d:%4d\n",++i,'\30');
          return 0;
 }

OUTPUT:

1:   1
2:   2
3:   3
4:   4
5:   5
6:   6
7:   7
8:  56    
9:  57
10:   8
11:   9
12:  10
13:  11
14:  12
15:  13
16:  14
17:  15
18: 312
19: 313
20:  16
21:  17
22:  18
23:  19
24:  20
25:  21
26:  22
27:  23
28: 568
29: 569
30:  24

Isn't '\1' equivalent to the character whose ASCII value is 1?
In the output, why are the numbers at 8,9,18,19,28,29... not in order? http://codepad.org/I1N6A71j

Upvotes: 1

Views: 1068

Answers (5)

user3623786
user3623786

Reputation: 1

Use hh before format d modificator needed!!! So you use d format it's assumes int32 value, but you use only char - 8 bit value!!! It's memory wrong accsss!!! Only c99 available or c++11. Or you need some intermediate value like that:

short val = '\1'; //for C 89
  printf("%d:%4hd\n",++i, val);
val =  '\2';
  printf("%d:%4d\n",++i,val);
......
or 
  printf("%d:%4hhd\n",++i,'\1'); //for c99 

Either don't work correctly!!!

Upvotes: 0

Shahbaz
Shahbaz

Reputation: 47603

This is the compiler output (MinGW):

a.c: In function 'main':
a.c:12:33: warning: unknown escape sequence: '\8'
a.c:13:33: warning: unknown escape sequence: '\9'
a.c:22:33: warning: multi-character character constant
a.c:23:33: warning: multi-character character constant
a.c:32:33: warning: multi-character character constant
a.c:33:33: warning: multi-character character constant

Some of these escape sequences are invalid in c. Escape sequences are in octal in C. You could also easily detect this if you had a decent editor. My ViM marks \8 and \9 in red because they are invalid.

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 477700

From 2.14.3:

The escape \ooo consists of the backslash followed by one, two, or three octal digits that are taken to specify the value of the desired character. The escape \xhhh consists of the backslash followed by x followed by one or more hexadecimal digits that are taken to specify the value of the desired character. There is no limit to the number of digits in a hexadecimal sequence. A sequence of octal or hexadecimal digits is terminated by the first character that is not an octal digit or a hexadecimal digit, respectively.

Since \8 and \18 are not valid sequences of octals, the meaning of those literals depends on your platform:

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

Upvotes: 2

kaeso
kaeso

Reputation: 341

Escape sequences are supposed to be in octal, thus \8 and \9 are not allowed and result in unspecified behavior. The result depends on the compiler you are using; in this case, it is ignoring the escape and handling '8' and '9' as plain ascii-char.

To get the proper (ascii-char) result, you should use \x8 and \x9.

Upvotes: 3

Roger Lipscombe
Roger Lipscombe

Reputation: 91965

The number after \ is treated as octal. I've no idea why the \8, \9, \19, etc. sequences are so odd, though. Since they're not valid octal numbers, the compiler is probably free to do anything with them.

Escape sequences can be specified in hex, if you'd prefer, as \x1, \x2, etc.

Upvotes: 0

Related Questions