Thursday, 28 July 2016

Initialization of Structure Variables

Structure Variable Initialization

struct student
char name[20];
    int rollno;
    float mark;
}S1={"aaaa", 111, 40};                                          // Method 1 

void main()
     struct student S2 = {"BBB", 222, 50};         // Method 2

    struct student S3, S4, S5;

    printf("\n\n Student1: \n\tName is %s \n\tRollno is %d \n\tMark is %f",, S1.rollno,  S1.mark );
    printf("\n\n Student2: \n\tName is %s \n\tRollno is %d \n\tMark is %f",, S2.rollno, S2.mark );

    S3 = S2;                // Method 3: Assigning one structure variable to another
    printf("\n\n Student3: \n\tName is %s \n\tRollno is %d \n\tMark is %f",, S3.rollno, S3.mark );

         // Method 4: Individual members initialization
          strcpy(, "DDD");
          S4.rollno = 444;
          S4.mark = 70;
          printf("\n\n Student4: \n\tName is %s \n\tRollno is %d \n\tMark is %f",, S4.rollno, S4.mark );

         // Method 5: Taking values from user
    printf("\nEnter name rollno and mark of a student5:");
    scanf("%s %d %f",, &S5.rollno, &S5.mark );
    printf("\n\n Student5: \n\tName is %s \n\tRollno is %d \n\tMark is %f",, S5.rollno, S5.mark );



        Name is aaaa
        Rollno is 111
        Mark is 40.000000

        Name is BBB
        Rollno is 222
        Mark is 50.000000

        Name is BBB
        Rollno is 222
        Mark is 50.000000

        Name is DDD
        Rollno is 444
        Mark is 70.000000

Enter name rollno and mark of a student5:       EEE       555          70
        Name is EEE
        Rollno is 555
        Mark is 70.000000

Goto Main Content:

No comments:

Post a Comment