Structures and Unions in C

  • Post author:
  • Post category:c
  • Post comments:0 Comments

Structures and Unions in C

In this lesson, you will learn about the different types of structures and unions in C programming. Structures and unions are used to group related data together and access them by name. You will learn how to declare, define, and access structures and unions. You will also learn about the different types of fields that can be used in structures and unions.

Example

#include <stdio.h> 

struct student 
{ 
    char name[50]; 
    int age; 
    float marks; 
}; 
  
int main() 
{ 
    struct student stu1; 
  
    strcpy(stu1.name, "John"); 
    stu1.age = 20; 
    stu1.marks = 90.5; 
  
    printf("Name: %s\n", stu1.name); 
    printf("Age: %d\n", stu1.age); 
    printf("Marks: %.2f\n", stu1.marks); 
  
    return 0; 
}

Output

Name: John
Age: 20
Marks: 90.50

Leave a Reply