-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,232
  • 3
  • 25
  • 44
clasimoes
  • 37
  • 1
  • 4

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,232
  • 3
  • 25
  • 44