C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 10)
10.
What will be the output of the program ?
#include<stdio.h>
int main()
{
struct byte
{
int one:1;
};
struct byte var = {1};
printf("%d\n", var.one);
return 0;
}
Discussion:
29 comments Page 1 of 3.
Sundar said:
1 decade ago
Hi Viewers,
If you store 1 in 1-bit field:
The left most bit is 1, so the system will treat the value as negative number.
The 2's complement method is used by the system to handle the negative values.
Therefore, the data stored is 1. The 2's complement of 1 is also 1 (negative).
Therefore -1 is printed.
Please read the solution given for an another problem, you will understand it better:
http://www.indiabix.com/c-programming/structures-unions-enums/discussion-372
If you store 1 in 1-bit field:
The left most bit is 1, so the system will treat the value as negative number.
The 2's complement method is used by the system to handle the negative values.
Therefore, the data stored is 1. The 2's complement of 1 is also 1 (negative).
Therefore -1 is printed.
Please read the solution given for an another problem, you will understand it better:
http://www.indiabix.com/c-programming/structures-unions-enums/discussion-372
Sivakumar said:
1 decade ago
int one:1 - means only one bit allocated to store 1.
Here pattern is 1.
Here MSB = LSB.
MSB = 1 means -ve value.
1 can be converted to 2's complement.
Than it's comes 1.
Than answer is -1.
Here pattern is 1.
Here MSB = LSB.
MSB = 1 means -ve value.
1 can be converted to 2's complement.
Than it's comes 1.
Than answer is -1.
Chitra said:
1 decade ago
For signed integers the leftmost bit will be taken for +/- sign, right?
So here 1 is treated as a sign, so it should print only a minus right? why is it printing -1? please anyone say?
So here 1 is treated as a sign, so it should print only a minus right? why is it printing -1? please anyone say?
Vasantha said:
1 decade ago
@Neeraja.
If we assign 2 to one(integer variable) then we get -2 as the answer because as int a:1, when we pass 2 as value we get 1000000000000010 which means -2.
If we assign 2 to one(integer variable) then we get -2 as the answer because as int a:1, when we pass 2 as value we get 1000000000000010 which means -2.
Codedamn said:
2 years ago
This happens because in C when using bit fields, the value -1 is typically used to represent true or on for 1-bit integers.
Janu said:
1 decade ago
Hi friends
lets we take 1 in 1-bit field:0001
so the left most bit is '1'
which indicates -ve value
so the ans is -1
lets we take 1 in 1-bit field:0001
so the left most bit is '1'
which indicates -ve value
so the ans is -1
D.shivashanker said:
1 decade ago
Here var is variable for struct byte type right, if here 2 is assigned to one (integer variable) what will happen?
Rajadurai said:
2 decades ago
Hi Priyanka, one bit is allocated for variable one. so it must took left most sign bit. so the ans is -1.
Neeraja said:
1 decade ago
@Shivashanker.
If we assign 2 to one then 2 gets printed because here the left most bit is "0" (0010).
If we assign 2 to one then 2 gets printed because here the left most bit is "0" (0010).
Aditi said:
1 decade ago
Can anyone please explain in detail?
What does a:1 mean?
How is this whole thing working?
What does a:1 mean?
How is this whole thing working?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers