Structures
Every C type covered so far — int, double, char, arrays — stores a single kind of value repeated any number of times. Real-world data rarely looks like that. A point has an x and a y. A student has a name, an ID, and a GPA. A date has a day, a month, and a year — three different pieces of information that belong together conceptually but have nothing to do with each other as raw types. C's answer to bundling values like this into one unit is the struct.
Declaring a structure type
A struct declaration defines a new, custom type made up of named members (also called fields), each with its own type. Declaring the struct does not create any variables by itself — it just teaches the compiler about the shape of the new type.
struct Point {
int x;
int y;
};With this declaration in scope, struct Point becomes a usable type, just like int or double. You can now declare variables of that type.
Declaring and initializing variables
#include <stdio.h>
struct Point {
int x;
int y;
};
int main(void) {
struct Point p1;
p1.x = 3;
p1.y = 7;
/* Initializer list: values are assigned to members in order. */
struct Point p2 = {10, 20};
printf("p1 = (%d, %d)\n", p1.x, p1.y);
printf("p2 = (%d, %d)\n", p2.x, p2.y);
return 0;
}p1 = (3, 7) p2 = (10, 20)
The dot operator (.) is how you read or write an individual member of a struct variable — p1.x means "the x member that lives inside p1."
Designated initializers (C99 and later)
Listing values positionally works, but it is fragile: if the struct gains a new member or the order changes, silent bugs can creep in. C99 introduced designated initializers, which name each member explicitly and can appear in any order.
struct Point p3 = { .y = 5, .x = 1 }; /* order does not matter */
struct Point p4 = { .x = 8 }; /* .y is zero-initialized */A struct with mixed member types
Struct members do not have to share a type. A single struct can mix integers, characters, arrays, floating-point numbers, and more — that is the entire point.
#include <stdio.h>
struct Student {
char name[50];
int id;
double gpa;
};
int main(void) {
struct Student s = { .name = "Ada", .id = 101, .gpa = 3.95 };
printf("%s (ID %d) has a GPA of %.2f\n", s.name, s.id, s.gpa);
return 0;
}Ada (ID 101) has a GPA of 3.95
typedef and structs, briefly
Writing struct Point every time you need the type is verbose. Many C codebases pair a struct declaration with typedef to give it a shorter name — this pattern gets its own dedicated page later, but it is common enough to preview here.
typedef struct {
int x;
int y;
} Point;
Point origin = {0, 0}; /* no "struct" keyword needed anymore */Structs are not objects
Comparing struct with related concepts
Concept | Groups data? | Has methods? | Access control? |
|---|---|---|---|
C | Yes | No | No |
C++ | Yes | Yes | Yes |
C array | Yes (same type only) | No | No |
C | Yes (overlapping memory) | No | No |
A struct is declared once and can then be used to create as many variables as needed.
Members are accessed with the dot operator:
variable.member.Initializer lists assign values positionally; designated initializers assign by name.
Uninitialized struct members (in designated initializers) default to zero.
Structs hold data only — no methods, no encapsulation.