Home > Software design >  Basic Types in C: legal way to write the number 65 assuming ASCII character set
Basic Types in C: legal way to write the number 65 assuming ASCII character set

Time:01-18

I was reading K.N.King's C Programming: A modern Approach and stumble upon this question on Basic Types.

Here's the original question: Which one of the following is not a legal way to write the number 65? (Assume that the character set is ASCII.)

(a) 'A'

(b) 0b1000001

(C) 0101

(d) 0x41

All four options seem legal to me and I was able to print the number '65' as such:

        char a, b, c, d;
        a = 'A';
        b = 0b1000001;
        c = 0101;
        d = 0x41;
        printf("%d\n", a);
        printf("%d\n", b);
        printf("%d\n", c);
        printf("%d\n", d);

The output:

65
65
65
65

What is the right answer then?

CodePudding user response:

I tried all four examples in my C interpreter:

ci> 'A'
65
ci> 0101
65
ci> 0x41
65
ci> 0b1000001
ci: error: Syntax error
ci> 

Now, on the one hand, this ci is not a robust, modern, conforming C implementation. But on the other hand, just like the C standard, it has never heard of 0b constants, which are a useful but nonstandard extension.

  •  Tags:  
  • Related