C Tutorial
C Typedef
typedef gives an existing type a shorter name. Use it for structs and function pointers.
A new name for an old type
typedef does not create a different kind of value. It attaches another name to a type you already have. After typedef struct Point Point;, you can write Point p instead ofstruct Point p. The layout is still the same struct.
Use it when a type is long or when you want the name to say what the value means. Do not hide a pointer behind a typedef until the rest of the file is already clear.
typedef a struct
First name the struct. Then alias it. After that, both struct Point and Point refer to the same type.
Example
#include <stdio.h>
struct Point {
int x;
int y;
};
typedef struct Point Point;
int main(void) {
Point p;
p.x = 3;
p.y = 4;
printf("(%d, %d)\n", p.x, p.y);
return 0;
}Without the typedef, every variable would be struct Point p. The alias is a spelling change. It does not allocate memory and it does not convert fields.
The common one-line form
You can pack the struct tag and the alias together. typedef struct Point { ... } Point;defines the struct and the short name in one declaration. Many headers use this.
Example
#include <stdio.h>
typedef struct Point {
int x;
int y;
} Point;
int main(void) {
Point a;
Point b;
a.x = 1;
a.y = 2;
b = a;
printf("%d %d\n", b.x, b.y);
return 0;
}Assignment copies every field. b = a is two ints moving, not a pointer. If you need two names for one object, store a Point * instead.
typedef a function pointer
The raw type int (*fp)(int, int) is easy to mistype. A typedef names it once. Aftertypedef int (*binop)(int, int);, a variable is just binop fp.
Example
#include <stdio.h>
typedef int (*binop)(int, int);
int add(int a, int b) {
return a + b;
}
int main(void) {
binop fp = add;
printf("%d\n", fp(3, 4));
return 0;
}This is optional. If you only have one function pointer in the file, the explicitint (*fp)(int, int) is still readable. Reach for the typedef when the same callback type appears in several signatures.
When the alias helps
- Structs you mention often:
Point,Player,Node. - Function pointer types you pass around: a comparator, a handler.
- Not every
int. A typedef namedcountthat is secretlyinthides more than it explains.
Compile these at /c/try. gcc accepts C17 typedefs in a single file. That editor is not
Next: the preprocessor, which edits the source text before gcc sees it.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.
Physics
A 2D vector type
Position, velocity, and force in a plane are all pairs of doubles. typedef struct { ... } Vector gives that pair a short name. |v| for (3, 4) is 5, the same geometry as every other 3-4-5 in this course.
|v| = √(x² + y²)
Example
#include <stdio.h>
#include <math.h>
typedef struct {
double x;
double y;
} Vector;
int main(void) {
Vector v = {3.0, 4.0};
printf("|v| = %.1f\n", sqrt(v.x * v.x + v.y * v.y));
return 0;
}Biology
A sample alias
A tube has an id and a mass. After typedef, every file can say Sample instead of repeating the struct layout. 0.250 g is a quarter of a gram — a small solid, not a beaker of water.
Example
#include <stdio.h>
typedef struct {
int id;
double mass_g;
} Sample;
int main(void) {
Sample s = {17, 0.250};
printf("id %d, %.3f g\n", s.id, s.mass_g);
return 0;
}