C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Structures

C Programming

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 thestructure 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