Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading