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 named count that is secretly int hides 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;
}

FAQ: C Typedef

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c typedef examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c typedef in this C C lesson (C Typedef).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.