The structure is a user-defined data type in C, which stores a collection of different data types. This tutorial guides you on how to use Structures in C.



  • The structure is similar to an array; the only difference is that the array stores the same data types.
  • struct keyword declares the structure in C.
  • Variables inside the structure are called members of the structure.

Defining a Structure in C

Syntax:

struct structureName
{
    //member definitions
};

Example:

struct Courses
{
    char  WebSite[50];
    char  Subject[50];
    int   Price;
};

Accessing Structure Members in C

Example:

#include<stdio.h>
#include<string.h>

struct Courses
{
    char  WebSite[50];
    char  Subject[50];
    int   Price;
};

void main( )
{
    struct Courses C;

    // Initialization
    strcpy( C.WebSite, "w3schools.in");
    strcpy( C.Subject, "The C Programming Language"); 
    C.Price = 0;

    // Print
    printf( "WebSite : %s\n", C.WebSite);
    printf( "Book Author : %s\n", C.Subject);
    printf( "Book Price : %d\n", C.Price);
}

Program Output:

WebSite : w3schools.in
Book Author : The C Programming Language
Book Price : 0


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram