-1

I'm trying to understand the following code in C:

struct values{
  int i:3;
  int j:3;
  int k:2;
};

int main(){
    struct values v = {-6,2,5};
    printf("%d %d %d", v.i,v.j,v.k);
}

This code produces the following output:

2 2 1

I'm trying to understand what does it mean the assignment for int values used inside the struct i.e.: int i:3 ?

I know that : is not an operator. So what does it do? Also, can someone explain how this output is achieved?

Thanks very much!

fredmaggiowski
  • 2,154
  • 2
  • 25
  • 44
clasimoes
  • 37
  • 1
  • 4
  • its declaring a bit-field, see here: https://en.wikipedia.org/wiki/Bit_field – thurizas Mar 23 '16 at 23:13
  • Why do you call it "assignment"? Where do you see any similarity to "assignment" here? – AnT Mar 23 '16 at 23:15
  • My mistake calling it assignement. It was my first impression but I really had no idea of the meaning. Thanks for giving me the right place to look! – clasimoes Mar 24 '16 at 01:29

1 Answers1

1

The numbers specifies the length in bits for each field.

Hence i and j are represented in 3 bits while k in 2 bits.

By the way this is question is clearly a duplicate of this question and there's a very good answer I suggest you to read.

Community
  • 1
  • 1
fredmaggiowski
  • 2,154
  • 2
  • 25
  • 44