🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C Notes
Topic #77

C typedef


C typedef

The typedef keyword lets you create a new name (an alias) for an existing type. This can make complex declarations easier to read, and your code easier to maintain.

For example, instead of always writing float, we can create a new type called Temperature to make the code clearer:

Example

#include <stdio.h>

typedef float Temperature;

int main() {
  Temperature today = 25.5;
  Temperature tomorrow = 18.6;

  printf("Today: %.1f C\n", today);
  printf("Tomorrow: %.1f C\n", tomorrow);

  return 0;
}

Here, Temperature is just another name for float. However, the code is more expressive: it tells us that these numbers are temperatures, not just any floating-point numbers.


Why Use typedef?

  • Simplifies code: Shorter, easier-to-read type names.
  • Improves clarity: Expresses intent better (for example, AGE instead of just int). Also helps avoid confusion when many variables share the same base type (like float or double).

Note: Good To Know: In modern C, typedef is often used together with struct, enum, and function pointers to keep code clean and easier to read.


typedef with struct

typedef can be useful with struct, because it lets you avoid writing struct every time:

Example

#include <stdio.h>

// Without typedef:
struct Car {
  char brand[30];
  int year;
};

// With typedef:
typedef struct {
  char brand[30];
  int year;
} Car;

int main() {
  struct Car car1 = {"BMW", 1999}; // needs "struct"
  Car car2 = {"Ford", 1969};          // shorter with typedef

  printf("%s %d\n", car1.brand, car1.year);
  printf("%s %d\n", car2.brand, car2.year);
  return 0;
}

This example shows how typedef makes it easier to work with multiple struct variables, like different car models:

Example

#include <stdio.h>

// With typedef
typedef struct {
  char brand[30];
  char model[30];
  int year;
} Car;

int main() {
  Car car1 = {"BMW", "X5", 1999};
  Car car2 = {"Ford", "Mustang", 1969};
  Car car3 = {"Toyota", "Corolla", 2011};

  printf("%s %s %d\n", car1.brand, car1.model, car1.year);
  printf("%s %s %d\n", car2.brand, car2.model, car2.year);
  printf("%s %s %d\n", car3.brand, car3.model, car3.year);

  return 0;
}

Here you can see how typedef keeps the code clean when structs are nested inside other structs:

Example

#include <stdio.h>

// Define three structs with typedef
typedef struct {
  char firstName[20];
  char lastName[20];
} Owner;

typedef struct {
  char brand[20];
  int year;
  Owner owner;
} Car;

typedef struct {
  char name[30];
  Car featuredCar;
} Dealership;

int main() {
  Owner person = {"John", "Doe"};
  Car car1 = {"Toyota", 2010, person};
  Dealership d = {"City Motors", car1};

  printf("Dealership: %s\n", d.name);
  printf("Featured Car: %s (%d), owned by %s %s\n",
    d.featuredCar.brand,
    d.featuredCar.year,
    d.featuredCar.owner.firstName,
    d.featuredCar.owner.lastName);

  return 0;
}

Note: It is up to you whether you want to use typedef or not. Your code will work the same without it. However, in modern C it is often used to make code shorter, clearer, and easier to maintain.

Want to go beyond the notes?

Join CodingNow 2.0's C course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C typedef – FAQs

Quick answers about learning C typedef in C.

This free note from CodingNow 2.0 explains C typedef in C — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C topic on CodingNow 2.0, including C typedef, is 100% free with no signup required.
With focused practice, most students grasp C typedef in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now