2,114 views
0 votes
0 votes
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); }
 

Please explain why result is 221

1 Answer

Best answer
4 votes
4 votes

Such questions should be solved considering the options given, because that way you would be able to judge the way the compiler is implemented.

Here we allocate 3 bits for p, 2 bits for c and 2 bits for m.

Since we are storing 2 which in binary is 10, storing it in 3 bits will give us 010 which still remains to be 2
similarly, we are storing -6 (2's complement of 0110 = 1010), in 2 bits, discarding the MSBs we have 10 which is 2
5 = (101)2  again discarding MSBs to fit into 2 bits gives 1 which in decimal notation remains to be 1

Hence 221

selected by

Related questions

0 votes
0 votes
2 answers
1
Debargha Mitra Roy asked Apr 10
103 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
3 votes
3 votes
3 answers
2
Laxman Ghanchi asked May 19, 2023
1,159 views
#include<stdio.h void print(int n) { printf("Hello "); if(n++ == 0) return ; print(n); n++; } int main() { void print(); print(-4); }How many times printf execute?? And H...
0 votes
0 votes
1 answer
3
Laxman Ghanchi asked May 19, 2023
685 views
#include<stdio.h>void print(int n){ printf("Hello "); if(n++ == 0) return ; print(n); n++;}int main(){ void print(); print(-4);}