Bit Fields


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    0


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    0



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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Compilation Error

    main.c:8:10: warning: declaration does not declare anything
    };
    ^
    main.c: In function ‘main’:
    main.c:14:9: error: ‘Un’ undeclared (first use in this function)
    Un.str.ch = 2;
    ^~
    main.c:14:9: 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>
    union Un
    {
    struct
    {
    unsigned char c : 1;
    unsigned int n : 2;
    }Stru;
    int ch;
    };
    int main()
    {
    union Un u;
    u.Stru.c = 1;
    printf("%d\n", u.Stru.c);
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    1



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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Compilation Error

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