1
#include <stdio.h>


int main()
{
    int a = '\12345';
    printf("%d",a);
    return 0;
}

Why is the printed result 5452853? I wonder what happened?

  • 3
    Related: https://stackoverflow.com/q/59471456/509868 – anatolyg Apr 10 '22 at 08:55
  • `'\12345'` is called a multi-character constant. As you can see here it is inherited by C from the B programming language: https://en.cppreference.com/w/c/language/character_constant (the values of each char in the constant initialize successive bytes of the resulting integer, in big-endian zero-padded right-adjusted order) – wohlstad Apr 10 '22 at 09:03
  • 3
    @wohlstad no the value is implementation defined. It's usually little endian on little endian machines and big endian on big endian machines but it's **not** guaranteed, that's why compilers always warn about it. In this case the value is equal to `'\123'*256*256 + '4'*256 + '5' = 0123*256*256 + 0x34*256 + 0x35` – phuclv Apr 10 '22 at 09:14
  • @phuclv - I took the quote about the value from cppreference - see https://en.cppreference.com/w/c/language/character_constant in the *Notes* section. Did I miss something ? – wohlstad Apr 10 '22 at 09:46
  • I don't quite understand the principle of this conversion yet, and I don't know if my idea is too simple. I thought the lower 32 bits of the binary number corresponding to '\12345' (010111000011000100110010001100110011010000110101) should be the same as the binary number corresponding to 5452853 (0000000000010100110011010000110101). – HuangGuojun Apr 10 '22 at 09:58
  • Where did you get the idea that this binary representation might be related to your integer value? – Gerhardh Apr 10 '22 at 10:21
  • 2
    @wohlstad you missed the most important part: *Although not specified by the C standard, most compilers...*. – Gerhardh Apr 10 '22 at 10:31
  • 1
    @HuangGuojun "I don't quite understand the principle of this conversion yet" <- You're missing that `'\123'` is a single character: `'\'` introduces an octal escape. (See C99 §6.4.4.4 for the details.) So the multi-character constant is built from three characters: `'\123'`, `'4'` and `'5'`. – Mark Dickinson Apr 10 '22 at 10:32
  • @Gerhardh - you are right of course. My mistake. – wohlstad Apr 10 '22 at 10:36
  • Your expected representation seems to be that `\123` are 4 separate characters. You can achieve that by escaping the `'\'`: `'\\12345'`. That will cause the compiler to see 6 characters instead of 4. And it will likely cause the warning `character constant too long for its type` instead of `multi-character character constant [-Wmultichar]` which you might get with your current code. – Gerhardh Apr 10 '22 at 10:40
  • I combined phuclv's and Mark Dickinson's answers and I seem to understand that first of all '\12345' is equivalent to three characters '\123' = 1*8*8+2*8+3*1 = 83; '4' = 52 ; '5' = 53. Then the result is equal to 83*256*256+52*256+53 = 5452853 – HuangGuojun Apr 10 '22 at 10:59

1 Answers1

0

I combined phuclv's and Mark Dickinson's answers and I seem to understand that first of all '\12345' is equivalent to three characters '\123' = 1*8*8+2*8+3*1 = 83; '4' = 52 ; '5' = 53. Then the result is equal to 83*256*256+52*256+53 = 5452853

– HuangGuojun

anatolyg
  • 26,506
  • 9
  • 60
  • 134