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

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.

Mahi said:   1 decade ago
Can anyone explain about the statement?

int one:1;

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.

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).

Sandeep said:   1 decade ago
Thanks prashant.

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

Ashok said:   1 decade ago
Here var is variable for struct byte type, here 1 is assigned to one (integer variable) ;.

Pranav said:   1 decade ago
What is this line means here ?

struct byte var = {1};

Vikas said:   1 decade ago
Thanks Sundar.

Abhinav said:   1 decade ago
Thanks sundar.


Post your comments here:

Your comments will be displayed after verification.