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 3 of 3.
Prashant o_O said:
1 decade ago
int a;
a=1; means 0000000000000001
and
int a:1;
a=1; means 1000000000000001
a=1; means 0000000000000001
and
int a:1;
a=1; means 1000000000000001
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
Parimal said:
1 decade ago
Thanks sundar.
Ganesh said:
1 decade ago
Thanks sundar
Deepa said:
1 decade ago
Thanks sundar.
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
Prasanna said:
1 decade ago
Can you explain clearly?
Uma said:
1 decade ago
Can any one explain this concept briefly?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers