Previous Next

C Structures

These structures help to have groups of different variables under a name. Example, a 'book' contains attributes such as title, author, publisher, no. of pages, pub_date, etc. For working on such kind of data, C offers the users a type called a structure.

Syntax

struct MyStructure {
   dataType member1;
   dataType member2;
};

Example:

#include <stdio.h>

struct Books
{
    char title[40];
    char author[40];
    float price;
};

int main()
{
    struct Books book1 = {"Macbeth", "William Shakespeare", 100.00};
    printf("Title of the book is %s\n", book1.title);
    printf("Author of the book is %s\n", book1.author);
    printf("Price of the book is %f\n", book1.price);
    return 0;
}

Output:

Title of the book is Macbeth
Author of the book is William Shakespeare
Price of the book is 100.00
Previous Next