C structure declaration

Declaring Structure Variable in C

In C we can group some of the user defined or primitive data types together and form another compact way of storing complicated information is called as Structure. Let us see how to declare structure in c programming language –

Syntax Of Structure in C Programming :

struct tag
{
   data_type1 member1;
   data_type2 member2;
   data_type3 member3;
};

Structure Alternate Syntax :

struct <structure_name>
{
    structure_Element1;
    structure_Element2;
    structure_Element3;
    ...
    ...
};

Some Important Points Regarding Structure in C Programming :

  1. Struct keyword is used to declare structure.
  2. Members of structure are enclosed within opening and closing braces.
  3. Declaration of Structure reserves no space.
  4. It is nothing but the “ Template / Map / Shape ” of the  structure .
  5. Memory is created , very first time when the variable is created / Instance is created.

Different Ways of Declaring Structure Variable :

Way 1 : Immediately after Structure Template

struct date
{
     int date;
     char month[20];
     int year;
}today;  

// 'today' is name of Structure variable

Way 2 : Declare Variables using struct Keyword

struct date
{
    int date;
    char month[20];
    int year;
};

struct date today;

where “date” is name of structure and “today” is name of variable.

Way 3 : Declaring Multiple Structure Variables

struct Book
{
    int pages;
    char name[20];
    int year;

}book1,book2,book3;

We can declare multiple variables separated by comma directly after closing curly.