How do I define a structure in C?

What is Structure: The structure is a user-defined data type that allows storing multiple types of data in a single unit. It occupies the sum of the memory of all members.
  • The structure member can be accessed only through the structure variable. 
  • Structure variables accessing the same structure but the memory allocated for each variable will be different.
Defining a Structure: The Struct keyword is used to define a structure.

Syntax of Structure: 

  1. Struct Structure_name 
  2. { 
  3. mebmer_variable1; 
  4. mebmer_variable2; 
  5. mebmer_variable3; 
  6. . 
  7. . 
  8. . 
  9. . 
  10. member_variable n; 
  11. }[structure variables]; 
        
  Let's see a simple example: 
  1. #include<stdio.h> 
  2. Struct students 
  3. { 
  4. char name[50]; // Structure members declaration. 
  5. int age; 
  6. }S1; // declaring structure variable of student. 
  7. int main() 
  8. { 
  9. printf("Enter the name of the student:\n"); 
  10. scanf("%s",S1.name); 
  11. printf("Enter the age of the student:\n"); 
  12. scanf("%d",S1.age); 
  13. printf("Name and age of the student: %s,%d,S1.name,S1.age"); 
  14. return 0; 
  15. } 
     The output of the following code is:
       
                   Enter the name of the student: Shubham 
                    Enter the age of the student:  23
                    Name and age of the student: Shubham,23

Comments

Popular posts from this blog

proassignment help backlink data index 41

programming shark backink data index 31

Programming shark backlink data index 67