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 1 of 3.

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

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.

Uma said:   1 decade ago
Can any one explain this concept briefly?

Prasanna said:   1 decade ago
Can you explain clearly?

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

Deepa said:   1 decade ago
Thanks sundar.

Ganesh said:   1 decade ago
Thanks sundar

Parimal said:   1 decade ago
Thanks sundar.

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

Prashant o_O said:   1 decade ago
int a;
a=1; means 0000000000000001

and

int a:1;
a=1; means 1000000000000001


Post your comments here:

Your comments will be displayed after verification.