`struct` — C Keyword

`struct` — C Keyword

The struct keyword in C: defines a composite type grouping named members.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

struct (C)

Defines a composite type that groups named members of possibly different types into a single unit.

Syntax

struct Name {
    Type member1;
    Type member2;
};

struct Name variable;
struct Name variable = { .member1 = val1, .member2 = val2 };   /* C99 designated init */

Example

#include <stdio.h>
#include <math.h>

struct Point {
    double x;
    double y;
};

double distance(struct Point a, struct Point b) {
    double dx = a.x - b.x;
    double dy = a.y - b.y;
    return sqrt(dx*dx + dy*dy);
}

int main(void) {
    struct Point p1 = {0.0, 0.0};
    struct Point p2 = {.x = 3.0, .y = 4.0};   /* C99 */

    printf("%.1f\n", distance(p1, p2));   /* 5.0 */
    return 0;
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}