Home / C Programming / Structures, Unions, Enums :: Discussion

Discussion :: Structures, Unions, Enums

  1. Point out the error in the program?

    #include 
    
     int main() 
     {  
        struct emp   
        {        
           char name[25];     
           int age;     
           float bs;     
       };
         sruct emp e;  
         e.name = "Suresh";   
         e.age = 25;    
         printf("%s %d\n", e.name,e.age);      
         return 0; 
     } 
    

  2. A.

    Error: Lvalue required/incompatible types in assignment

    B.

    Error: invalid constant expression

    C.

    Error: Rvalue required

    D.

    No error, Output: Suresh 25

    View Answer

    Workspace

    Answer : Option A

    Explanation :

    We cannot assign a string to a struct variable like e.name = "Suresh"; in C.

    We have to use strcpy(char *dest, const char *source) function to assign a string.

    Ex: strcpy(e.name, "Suresh");


Be The First To Comment