Bit Fields


  1. What will be the output of the following C code?
    #include <stdio.h>
    union Un
    {
    struct
    {
    unsigned char ch : 2;
    unsigned int n : 2;
    }s;
    int ch;
    };
    int main()
    {
    union Un u = {2};
    printf("%d\n", u.s.ch);
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    2


  1. What will be the output of the following C code?
    #include <stdio.h>
    union N
    {
    struct
    {
    unsigned char c : 2;
    unsigned int num : 2;
    }M;
    int c;
    };
    int main()
    {
    union N n.M = {2};
    printf("%d\n", n.M.c);
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Compilation Error

    main.c: In function ‘main’:
    main.c:13:18: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    union N n.M = {2};
    ^
    main.c:13:18: error: expected expression before ‘.’ token
    main.c:14:24: error: ‘n’ undeclared (first use in this function)
    printf("%d\n", n.M.c);
    ^
    main.c:14:24: note: each undeclared identifier is reported only once for each function it appears in



  1. What will be the output of the following C code?
    #include <stdio.h>
    struct Stru
    {
    unsigned int ch : 2;
    unsigned int n : 2;
    };
    int main()
    {
    struct Stru s;
    s.ch = 3;
    s.n = 1;
    printf("%d\n", sizeof(s));
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    4


  1. What will be the output of the following C code?
    #include <stdio.h>
    struct St
    {
    unsigned int c : 2;
    unsigned int n : 2;
    };
    int main()
    {
    struct St s;
    s.c = 13;
    s.n = 10;
    printf("%d\n", s.n);
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    2



  1. What will be the output of the following C code?
    #include <stdio.h>
    struct Stru
    {
    unsigned int x : 7;
    unsigned int y : 2;
    };
    int main()
    {
    struct Stru s;
    s.x = 110;
    s.y = 2;
    printf("%d\n", s.x);
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    None of these