The structure is a user-defined data type in C, which is used to store a collection of different kinds of data.
- The structure is something similar to an array; the only difference is array is used to store the same data types.
- struct keyword is used to declare 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