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?

Re: Using char in bit fields structure. 80 80

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.

Re: Using char in bit fields structure. 80 80

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?

Re: Using char in bit fields structure. 80 80

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.

Re: Using char in bit fields structure. 80 80

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
Re: Using char in bit fields structure. 80 80

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?

Re: Using char in bit fields structure. 80 80

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.

Re: Using char in bit fields structure. 80 80

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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.