Working with Structures in C Programming


In C programming, a structure (also called a struct) is a collection of related variables, known as members, grouped together under a single name. It is a user-defined data type that can be used to store a group of data items of different data types.
A structure is defined using the keyword "struct" followed by the name of the structure and a list of members enclosed in curly braces.

   Syntax :
        struct structure_name
        {
           datatype member_1 ;
           datatype member_2 ;
           datatype member_n ;
        }

  • This program is an example of how to use structures in C. It defines a structure called "student" with three members: name (a character pointer), age (an integer), and per (a float).
  • In the main function, it creates two variables of the struct student type "o" and "o2"
  • Then it assigns values to the members of the "o" struct variable. The member 'name' is assigned with a string "Tutor Joes" and 'age' is assigned with 30 and 'per' is assigned with 85.5
  • Then it uses the printf statement to print the values of the members of the "o" struct variable using the dot operator (.) to access them.

Source Code

//Structure in C
#include<stdio.h>
struct student
{
    char *name;
    int age;
    float per;
};
int main()
{
    struct student o,o2;
   /*
    printf("\nSize of Struct : %d",sizeof(o));//4
    printf("\nSize of char : %d",sizeof(char));//4
    printf("\nSize of int : %d",sizeof(int));//4
    printf("\nSize of float : %d",sizeof(float));//4
    */
    o.name="Tutor Joes";
    o.age=30;
    o.per=85.5;
 
    printf("\nName        : %s",o.name);
    printf("\nAge         : %d",o.age);
    printf("\nPercent     : %f",o.per);
 
    return 0;
}
 
To download raw file Click Here

Output

Name        : Tutor Joes
Age         : 30
Percent     : 85.500000

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C