C Workshop · Point Struct
Lesson C

Point Struct

Make a struct Point with x and y, set (3, 4), print both.

What it is

A Point has two integers, x and y. You set them to 3 and 4 and print both. That is the whole geometry of a 2-D position in its smallest C form.

Approach

  1. Define struct Point { int x; int y; };
  2. Set p.x and p.y.
  3. printf both.

Where this is used in the real world

Diagram

1

p

Point

2

.x

3

3

.y

4

4

stdout

3 4

Run algorithm

Each step highlights the matching C line. Program output appears only when a line that prints actually runs.

Ready
C code
1#include <stdio.h>
2struct Point {
3    int x;
4    int y;
5};
6int main(void) {
7    struct Point p;
8    p.x = 3;
9    p.y = 4;
10    printf("%d %d\n", p.x, p.y);
11    return 0;
12}
What this line does
Press Run algorithm. The highlight will move through the C code.
Program output
(no output yet)

Check your understanding

Five questions. Pick one answer each, then Check answers. The correct choice turns green.

1. Output:

p.x and p.y.

2. The dot operator:

.

3. struct Point p; without assign:

Assign first.

4. A nested kind: Point inside a Player would be:

Nested dot.

5. Two %d codes print:

Format list.