1

I am beginner in C and can't understood how this piece of code is working:

struct marks{
  int p:3;
  int c:3;
  int m:2;
};

void main(){
  struct marks s={2,-6,5};
  printf("%d %d %d", s.p, s.c, s.m); 
}

I found that the output is: 2 2 1

But after a lot of try I was unable to figure out how the output is like that. I have less knowledge about the struct. That's why may be I am feeling some problem here.

  • Possible duplicate of [When to use bit-fields in C?](https://stackoverflow.com/questions/24933242/when-to-use-bit-fields-in-c) – Gaurav Pathak Jun 23 '17 at 10:32
  • 2
    The int `c` and `p` have only 3 bits. With 3 bits you have only 8 possible values: from 0 to 7 using `unsigned int`, from 0 to 3 and from -1 to -4 using (signed) `int`. You cannot insert the value -6 into a 3 bit `int`, if you assign it only the 3 less significant bits are set into the variable. – Sir Jo Black Jun 23 '17 at 10:37
  • 1
    What did you expect? Why do you think you can put two buckets full of water into a single bucket? That's not even related to bit-fields, but simply using too small integer types. Did you enable compiler wanrnings? Why not? If yes: didn't the complier warn? Why did you ignore them? Your code relies on implementation defined conversions. – too honest for this site Jun 23 '17 at 11:05

2 Answers2

3

Well, inside the struct you defined- p:3, c:2, m:2 are actually denoting the bit fields. To know more about bit fields in C go through this link in SO or this link in Wikipedia.

For simplicity, know that the 3 or 2 after the colon(:) sign is representing bit-fields of width 3 or 2. That means 3 or 2 adjacent computer memory locations which have been allocated to hold a sequence of bits.

Now, inside the main() function of your code:

struct marks s={2,-6,5};

Here,

Binary value of 2: 00000010

Binary value of -6: 11111010

Binary value of 5: 00000101

Now according to bit-fields, from binary value of 2, we will take last 3 digits which is 010 (in decimal it is 2) and from binary value of -6, we will take last 2 digits which is 10 (in decimal it is 2) and from binary value of 5, we will take last 3 digits which is 01 (in decimal it is 1) and finally assign them to p, c or m.

That's how the output comes as 2 2 1.

Hope, I could make you clear.

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
  • 2
    C does not support _methods_. An `main` is not a _method_ in C++ either. And your explanation is not quite correct. The result is implementation defined, there is no guarantee for this result. – too honest for this site Jun 23 '17 at 11:07
  • 1
    Thanks.that was my mistake(it would be main() function).edited. – UkFLSUI Jun 23 '17 at 11:16
  • 1
    But how the result is not guaranteed. Can you make it more clear? It will be helpful for everyone. Thanks. – UkFLSUI Jun 23 '17 at 11:19
2

first of all your declaration of struct is using bit fields.

struct marks{
int p:3;
int c:3;
int m:2;
};

here p:3 means we are defining P to be an int and we are preserving only 3 bit of the given number. For example , You need 3 bits to represent the value 5, which would be represented as 101. The lower 2 bits are the value 1. That's why you get m=1.

This link will give you more explanation.

Thanks.

Mohiuddin Sumon
  • 103
  • 2
  • 13