Menu
Coddy logo textTech

Declaring a Struct

Part of the Logic & Flow section of Coddy's C journey — lesson 39 of 63.

Now that you understand what a struct is, it's time to learn how to create your own structure types. Declaring a struct in C follows a specific syntax that defines the blueprint for your custom data type.

The basic syntax for declaring a struct is:

struct StructureName {
    datatype member1;
    datatype member2;
    // ... more members
};

Here's how each part works: you start with the struct keyword, followed by a name for your structure, then list all the member variables inside curly braces. Each member is declared just like a regular variable with its data type and name. Don't forget the semicolon after the closing brace!

For example, to create a structure that represents a point in 2D space:

struct Point {
    int x;
    int y;
};

This declaration creates a new data type called struct Point that contains two integer members: x and y. Once declared, this structure type can be used throughout your program to create variables that hold coordinate information in an organized way.

challenge icon

Challenge

Easy

Create a C program that defines a struct to represent a book in a library system. Your program should:

  1. Define a struct named Book that contains the following members:
    • An integer id to store the book's identification number
    • A character array title with size 50 to store the book's title
    • An integer pages to store the number of pages
    • A float price to store the book's price
  2. After defining the struct, print a confirmation message: Book struct defined successfully!
  3. Print the size of the Book struct in bytes using sizeof() in the format: Size of Book struct: [size] bytes
  4. Print the size of each member individually:
    • Size of id: [size] bytes
    • Size of title: [size] bytes
    • Size of pages: [size] bytes
    • Size of price: [size] bytes

This challenge tests your understanding of struct declaration syntax, including the proper use of the struct keyword, member declarations with appropriate data types, and the semicolon after the closing brace. You'll also explore how C allocates memory for struct members using the sizeof() operator.

Remember that the exact size values may vary depending on your system's architecture, but the struct definition syntax must be correct for the program to compile and run successfully.

Cheat sheet

To declare a struct in C, use the following syntax:

struct StructureName {
    datatype member1;
    datatype member2;
    // ... more members
};

Start with the struct keyword, followed by a name for your structure, then list all member variables inside curly braces. Each member is declared with its data type and name. Don't forget the semicolon after the closing brace!

Example of a Point structure:

struct Point {
    int x;
    int y;
};

This creates a new data type called struct Point with two integer members that can be used throughout your program.

Try it yourself

#include <stdio.h>

int main() {
    // TODO: Define your Book struct here
    
    
    // Print confirmation message
    printf("Book struct defined successfully!\n");
    
    // TODO: Print the size of the Book struct and its members
    
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow