0

What is CHAR_BIT==16 means in this code? It doesn't compile, I can not figure out the reason and what will be in puts so code will compile?

    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    static_assert(CHAR_BIT==16,"16 bit falsely assumed");
    int main()
    {
        puts("hello world this");
        return 0;
    }
4
  • CHAR_BIT tells you how many bits in a byte. Commented Mar 11, 2022 at 13:52
  • If you found static_assert(CHAR_BIT==16, ...); in some code, there should be some hints as to why that check is done somewhere close to where you found that assertion. Commented Mar 11, 2022 at 13:57
  • 1
    @TedLyngmo False. See 7.2p1 of C11. Commented Mar 11, 2022 at 14:00
  • @dbush Yey! _Static_assert (and static_assert via a macro in assert.h I suppose) - Wow, that made my day. I've wanted that many times but just assumed ... stoopid me. Many thanks! Commented Mar 11, 2022 at 14:02

2 Answers 2

4

CHAR_BIT is a Macro defined in limits.h. It mentions the number of bits in a char. Most systems use 8 bits, but there are architectures with less (7 bits) or more.

In your code, the assert is checking, if the system is using 16 bits for a char, then only the code will compile.

Note: Based on your compiler version and support, you may need to use _Static_assert instead.

Sign up to request clarification or add additional context in comments.

2 Comments

There are no C implementations that conform to the C standard where CHAR_BIT is 7. C 2018 5.2.4.2.1 1 requires it be at least 8.
@EricPostpischil agree, what I meant is there are systems with bytes using 7 bits (not necessarily conforming).
2

CHAR_BIT is defined in <limits.h>. You need to include it for your code to compile.

1 Comment

...but even then CHAR_BIT is hugely unlikely to be 16. If it is, I would love to hear about the OP's platform.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.