Home » C Programming » Typedef » Question
  1. What will be the output of the following C code?
    #include <stdio.h>
    typedef struct B
    {
    int n1, n2;
    };
    int main()
    {
    B k = {10, 20};
    printf("%d\n", k.n1);
    }
    1. 10
    2. 20
    3. Compilation Error
    4. Undefined behaviour
    5. None of these
Correct Option: C

Compilation Error

main.c:5:5: warning: useless storage class specifier in empty declaration
};
^
main.c: In function ‘main’:
main.c:8:9: error: unknown type name ‘B’; use ‘struct’ keyword to refer to the type
B k = {10, 20};
^
struct
main.c:8:20: warning: excess elements in scalar initializer
B k = {10, 20};
^~
main.c:8:20: note: (near initialization for ‘k’)
main.c:9:25: error: request for member ‘n1’ in something not a structure or union
printf("%d\n", k.n1);



Your comments will be displayed only after manual approval.