AnyCompiler

Structs

A struct groups related fields under one named type. Unlike a class in Java, C++, or every other language on this site, a struct has no methods, no constructors, and no access control — it's purely a way to bundle data together, and every field is directly accessible.

#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point p = {3, 4};
    printf("%d %d\n", p.x, p.y);
    return 0;
}
Output
3 4

Next: dynamic memory — malloc, free, and the heap: the mechanism every other language's garbage collector exists to automate away.

Practice exercise

Define a struct Book with fields title (a char array of size 50) and pages (int). Create a Book with title "Dune" and pages 412, then print both with printf.