C Language Structs Simple data structures

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Structure data types are useful way to package related data and have them behave like a single variable.

Declaring a simple struct that holds two int members:

struct point 
{
    int x;
    int y; 
};

x and y are called the members (or fields) of point struct.

Defining and using structs:

struct point p;    // declare p as a point struct
p.x = 5;           // assign p member variables
p.y = 3;

Structs can be initialized at definition. The above is equivalent to:

struct point p = {5, 3};

Structs may also be initialized using designated initializers.

Accessing fields is also done using the . operator

printf("point is (x = %d, y = %d)", p.x, p.y);


Got any C Language Question?