C - Structures

C Programming

Share

In this tutorial we will learn about structures in C programming language.

If we want to store a single value in C we can use any of the fundamental data types like int, float etc.

And if we want to store values of same data type under one variable name then we take help of an array.

But we can't store different data type values that are logically related using simple variable or arrays. For this we take help of structure denoted by the struct keyword.

Syntax of a structure

Following is the syntax of a structure.

struct tagName {
  dataType member1;
  dataType member2;
};

Defining a structure

We use the struct keyword to define a structure in C programming language.

In the following example we have a student structure which consists of firstname, lastname, id and score of a student.

struct student {
  char firstname[64];
  char lastname[64];
  char id[64];
  int score;
};

Points to note!

  • In the above example we have defined a structure using the struct keyword. The name of the structure is student and is also referred as the structure tag.
  • The student structure consists of four data fields namely firstname, lastname, id and score. These data fields are also known as the structure elements or members.
  • The structure template ends with a semicolon.

The members of a structure are not variables and do not occupy any memory space themselves until we create a structure variable.

Creating structure variables

To create a structure variable we use the structure tag name.

In the following example we are creating a structure variable std1 of type student structure.

// student structure template
struct student {
  char firstname[64];
  char lastname[64];
  char id[64];
  int score;
};

// creating student structure variable
struct student std1;

Accessing the members of a structure

To access the members of a structure we use the structure variable name and the . member operator followed by the name of the member.

In the following example we are printing out the id of the std1 structure variable.

printf("ID: %s\n", std1.id);

Write a program in C to get student details from the user and store it in a structure variable and then print the details

In the following example we will use the same student structure that we have discussed so far in this tutorial. Feel free to add some more members as per your need.

#include <stdio.h>

int main(void) {
  // creating a student structure template
  struct student {
    char firstname[64];
    char lastname[64];
    char id[64];
    int score;
  };
  
  // creating a student structure variable
  struct student std1;
  
  // taking user input
  printf("Enter First Name: ");
  scanf("%s", std1.firstname);
  
  printf("Enter Last Name: ");
  scanf("%s", std1.lastname);
  
  printf("Enter ID: ");
  scanf("%s", std1.id);
  
  printf("Enter Score: ");
  scanf("%d", &std1.score);
  
  // output
  printf("\nStudent Detail:\n");
  printf("Firstname: %s\n", std1.firstname);
  printf("Lastname: %s\n", std1.lastname);
  printf("ID: %s\n", std1.id);
  printf("Score: %d\n", std1.score);
  return 0;
}

Output:

Enter First Name: Yusuf
Enter Last Name: Shakeel
Enter ID: student-01 
Enter Score: 8 

Student Detail:
Firstname: Yusuf
Lastname: Shakeel
ID: student-01
Score: 8
Share