Structure is a user-defined data type in C which allows you to combine different data types to store a particular type of record. Structure helps to construct a complex data type in more meaningful way. It is somewhat similar to an Array. The only difference is that array is used to store collection of similar datatypes while structure can store collection of any type of data.
Struct keyword is used create structure in C programming.
Structure is used to represent a record. Suppose you want to store record of Student which consists of student name, address, roll number and age. You can define a structure to hold this information.
Refer following example to understand working and use of structure.
Refer following example to understand working and use of structure.
#Example:
  #include <stdio.h>
  struct student
  {
     char name[50];
     char add[50];
     int roll;
     int age;
  } s;
  void main()
  {
     printf("Enter information:\n\n");
     printf("Enter name: ");
     scanf("%s", s.name);
     printf("Enter address: ");
     scanf("%s", s.add);
     printf("Enter roll number: ");
     scanf("%d", &s.roll);
     printf("Enter age: ");
     scanf("%d", &s.age);
     printf("\n\nDisplaying Information:\n\n");
     printf("Name: ");
     puts(s.name);
     printf("Address: ");
     puts(s.add);
     printf("Roll number: %d\n",s.roll);
    printf("Age: %d\n", s.age);
  }
Output
Enter information:
Enter name: Pankaj Enter address: Nanded Enter roll number: 22 Enter age: 22
Displaying Information:
Name: Pankaj Address: Nanded Roll number: 22 Age: 22

 
 
 
 
 
0 comments:
Post a Comment