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;
}
1
-1
0
Error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
29 comments Page 3 of 3.

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

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

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.

Priyanka said:   2 decades ago
Can any 1 explain me o/p of this question?


Post your comments here:

Your comments will be displayed after verification.