I'm writing a cross-platform code and I need to be sure the following won't make me trouble.

struct X
{
   unsigned char a:3;
   unsigned char b:5;
};

Is using other then int types is part of the ANSI C?
Will it be compiled with every compiler?

Recommended Answers

All 7 Replies

Is using other then int types is part of the ANSI C?

The behavior is implementation-defined if you use anything other than _Bool, signed int, or unsigned int.

The behavior is implementation-defined if you use anything other than _Bool, signed int, or unsigned int.

Do you mean the behavior of the compiler is implementation defined??
Can you clarify?

I mean anything else is non-portable. The compiler is free to allow or disallow types beyond int (signed or unsigned) and _Bool, but it must document which types are allowed beyond the ones required by the standard.

It may not be guaranteed to be portable by the C standard, but I've never heard of a C compiler that doesn't have signed and unsigned char's.

I'm sure there's some minimalist one that is that way, but it's designed for embedded applications, I'd bet $$$.

commented: void main is harmless 99.9% of the time, but we don't cut any slack for it. -4

Thanks for the answers, but maybe my question wasn't clear.
I'm asking specifically regarding bit fields in struct. All the compilers I work with do have signed and unsigned chars in general, but I want to be sure that it's ok to use them in bit fields. Is that what you meant?

Thanks for the answers, but maybe my question wasn't clear.
I'm asking specifically regarding bit fields in struct.

Your question was perfectly clear, and my answer is equally clear. Your bit fields can use _Bool and int (signed or unsigned) as the base type without any portability risk. Anything else is not portable.

Your question was perfectly clear, and my answer is equally clear. Your bit fields can use _Bool and int (signed or unsigned) as the base type without any portability risk. Anything else is not portable.

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.