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;
}
Try it Yourself »
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 justint
). Also helps avoid confusion when many variables share the same base type (likefloat
ordouble
).
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;
}
Try it Yourself »
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;
}
Try it Yourself »
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;
}
Try it Yourself »
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.