[interfacekit] Re: HELP!

It's a bitfield. It declares test as a struct with members which are 1 bit, 2 bits, and 3 bits wide. The funky output is the result of the value getting truncated to fit the bit widths. For example, in the first assignment, 7 (binary: 111) ends up like this:

x.a == 1 (binary: 1)
x.b == 3 (binary: 11)
x.c == 7 (binary: 111)

The second assignment is weird because the value gets chopped down to a single bit by the assignment to x.a before getting assigned to x.c.

HTH,

e

Adi Oanca wrote:

Hi!



I encountered this sample and I can't understand what it does! I searched(my archive of C books) but found no references to something like this, so PLEASE help me understand what this means and what happens here...


#include<stdio.h>

struct test{

unsigned a:1, b:2, c:3;

};

void main(){

struct test x;

x.a = x.b = x.c = 7;

printf("%d %d %d\t", x.a, x.b, x.c);

x.c = x.a = x.b = 7;

printf("%d %d %d", x.a, x.b, x.c);

}

The result is: 1 3 7 1 3 1.

What's the meaning of: a:1, b:2, c:3 ?


Thank you, Adi.



---------------------------------------------------------------
Cauta-ti perechea pe http://dating.acasa.ro




Other related posts: