0

I was practicing some C related MCQ's and I came across a question to predict output. Here's the question:

#include <stdio.h>

int main(void) {
    char a = '012';
    char b = '\012';
    printf("%d %d", a, b);
    return 0;
}

The Output given was: 50 10

Then I added following more lines and executed on gcc compiler

#include <stdio.h>
int main(void) {
    char a = '\012';
    char b = '\08';
    char c = '012';
    char d = '\0x12';
    printf("%d %d %d %d", a, b, c, d);
    return 0;
}

and the output I got was 10 56 50 50 (See this)

For some variables I think the ASCII of last char is assigned but not for all. Can anyone explain this behavior and How values are assigned ? And it would be helpful if anyone can quote references for these.

abhishekkannojia
  • 2,796
  • 23
  • 32
  • 1
    Read about [multicharacter litera](http://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters) – Mohit Jain Jun 26 '14 at 07:35
  • @unwind How is this duplicate to the one you marked, that was about multichar char scope and uses. The one *Mohit Jain* quoted is more appropriate. Besides I want to know that if they are multichar why sometimes *ASCII* of last char is assigned and sometimes some other value. – abhishekkannojia Jun 26 '14 at 07:46
  • @abhishekkannojia I disagree. The point is that the behavior of multichar literals is implementation-defined. – unwind Jun 26 '14 at 07:51

0 Answers0