Showing posts with label Variable Initialization. Show all posts
Showing posts with label Variable Initialization. Show all posts

Thursday, 28 July 2016

Initialization of Structure Variables

Structure Variable Initialization


#include<stdio.h>
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.name, S1.rollno,  S1.mark );
    printf("\n\n Student2: \n\tName is %s \n\tRollno is %d \n\tMark is %f", S2.name, 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.name, S3.rollno, S3.mark );

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

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

}

Output:

 Student1:
        Name is aaaa
        Rollno is 111
        Mark is 40.000000

 Student2:
        Name is BBB
        Rollno is 222
        Mark is 50.000000

 Student3:
        Name is BBB
        Rollno is 222
        Mark is 50.000000

 Student4:
        Name is DDD
        Rollno is 444
        Mark is 70.000000

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


Goto Main Content:
https://tejaswinihbhat.blogspot.in/2016/07/data-structures-15CS33-Module1.html
https://tejaswinihbhat.blogspot.in/2016/07/structures.html

Structures