🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →C typedef Basics Explained
Lecture 49 of our free C programming course: create readable type names, avoid macro traps, and write cleaner embedded C programming code.
Chapter 10
Structure and Union
Level
Beginner
Reading time
10 minutes
The C typedef keyword gives an existing type a new name. It does not create a new type, it does not allocate memory, and it costs nothing at run time, yet it is one of the most important tools for readable firmware. Open any microcontroller header file and you will find typedefs everywhere: uint8_t, size_t, register types, handle types. In this free C tutorial lecture you will learn the exact syntax, see the rules that surprise beginners, and learn why typedef is safer than a #define for types. It is a core topic of any free embedded C course.
What You Will Learn
- The typedef syntax and how to read it
- Why typedef creates an alias, not a new type
- How to give several names to one type in a single statement
- Why typedef cannot be combined with static or extern
- typedef versus #define for pointer types
- Naming conventions used in embedded projects
Prerequisites
You should know the basic data types, declarations and pointers. A working gcc compiler lets you run every example in this lecture of our free C programming course.
What C typedef Does
The general form is easy to remember: write a normal variable declaration, then put the word typedef in front. The name that would have been the variable now becomes the new type name.
typedef existing_type new_name;
Because the alias and the original are the same type, they mix freely. A variable of type ticks_t can be passed to a function expecting unsigned long with no conversion warning. This is both a strength (easy porting) and a limitation (typedef gives you no extra type checking).
A First Working Example
The program below defines several aliases that an embedded programmer might really use, shows a typedef that declares three names at once, and compares a typedef pointer against a macro pointer.
/* file: typedef_basic.c */
#include <stdio.h>
#include <stdint.h>
typedef uint8_t byte_t;
typedef unsigned long ticks_t;
typedef float volts_t;
typedef int adc_raw_t, dac_code_t, pwm_duty_t;
typedef int *int_ptr_t;
#define INT_PTR_MACRO int *
int main(void)
{
byte_t b = 200;
ticks_t t = 123456UL;
volts_t v = 3.3f;
adc_raw_t raw = 2048;
dac_code_t code = raw / 2;
pwm_duty_t duty = 75;
int x = 10;
int_ptr_t a, c;
INT_PTR_MACRO m, n;
a = &x;
c = &x;
m = &x;
n = x; /* n is a plain int: the macro only expanded to "int *m, n" */
printf("byte=%u ticks=%lu volts=%.1f\n", b, t, v);
printf("raw=%d code=%d duty=%d\n", raw, code, duty);
printf("sizeof(a)=%zu sizeof(c)=%zu sizeof(m)=%zu sizeof(n)=%zu\n",
sizeof a, sizeof c, sizeof m, sizeof n);
printf("*a=%d *c=%d *m=%d n=%d\n", *a, *c, *m, n);
return 0;
}
$ gcc -std=c11 -Wall -Wextra -o typedef_basic typedef_basic.c
$ ./typedef_basic
byte=200 ticks=123456 volts=3.3
raw=2048 code=1024 duty=75
sizeof(a)=8 sizeof(c)=8 sizeof(m)=8 sizeof(n)=4
*a=10 *c=10 *m=10 n=10
The sizes shown assume a 64-bit PC, where a pointer is 8 bytes. On a 32-bit microcontroller the pointer sizes would print 4.
typedef Versus #define for Types
Look at the two last lines of the program. With int_ptr_t a, c; both variables are pointers (8 bytes each). With the macro, INT_PTR_MACRO m, n; expands to int * m, n;, so only m is a pointer and n is an ordinary int (4 bytes). This is the classic reason professionals prefer typedef for types.
| Feature | typedef | #define |
|---|---|---|
| Handled by | The compiler | The preprocessor (text replacement) |
| Respects scope | Yes, block and file scope | No, from definition to end of file |
| Declares several pointers correctly | Yes | No, only the first name gets the star |
| Can name arrays, functions and structures cleanly | Yes | Awkward or impossible |
| Appears in debugger and error messages | Yes, as a type name | No, the macro text is already gone |
C typedef Rules and Restrictions
Multiple Names in One Statement
One typedef statement may introduce several names for the same type, as shown by adc_raw_t, dac_code_t and pwm_duty_t. This is handy but use it sparingly, because three names for one type can hide the real meaning of a value.
No Storage Class Allowed
Syntactically, typedef is classed as a storage-class specifier, so you cannot combine it with static, extern, register or auto in the same declaration. A typedef only names a type; where a variable lives is decided when you declare the variable. Here is what the compiler says:
/* file: typedef_bad.c */
typedef static int bad_t;
typedef int ok_t;
int main(void) { ok_t v = 1; return v - 1; }
$ gcc -std=c11 -c typedef_bad.c
typedef_bad.c:1:1: error: multiple storage classes in declaration specifiers
1 | typedef static int bad_t;
| ^~~~~~~
Fix: create the typedef first, then declare the variable as static bad_t counter;. The storage class belongs to the variable, not to the type name.
Scope and Placement
A typedef follows normal scope rules. Put shared typedefs in a header file so every source file sees the same name. A typedef declared inside a function is visible only in that block.
Why Embedded Projects Use C typedef
- Portability: change
typedef uint16_t sample_t;touint32_tin one place when a new ADC arrives. - Self-documenting code:
volts_ttells the reader more thanfloat. - Fixed-width types:
uint8_t,int32_tand friends in stdint.h are typedefs themselves. - Opaque handles: a driver exposes
uart_handle_tand hides what is inside.
| Convention | Example | Note |
|---|---|---|
| Suffix _t | ticks_t, volts_t | Common in firmware, but POSIX reserves _t names, so add a project prefix in large codebases |
| Project prefix | ep_ticks_t | Avoids collisions with library names |
| Fixed-width base type | uint16_t | Prefer over plain int for hardware data |
Common Mistakes
- Thinking typedef creates a distinct type and expecting the compiler to reject
adc_raw_tmixed withdac_code_t. They are both justint. - Using
#definefor pointer types and being surprised by the second variable. - Writing
typedef staticortypedef extern. - Hiding a pointer behind a typedef so callers cannot see the star. This makes
constconfusing: aconst int_ptr_tis a constant pointer, not a pointer to constant data. - Defining the same typedef differently in two headers. C11 allows redefinition only if both are identical.
Best Practices
typedef checklist
- Use typedef, never #define, to name types.
- Name by meaning (
volts_t), not by representation (float_t). - Keep shared typedefs in one header with include guards.
- Avoid typedefs that hide pointers unless the type is a deliberate opaque handle.
Performance and Safety Considerations
typedef has zero run-time cost because it disappears after compilation. The generated machine code for ticks_t t is identical to unsigned long t. The real benefit is long-term safety: when a sensor upgrade needs a 32-bit sample instead of 16-bit, you edit one typedef instead of hunting through hundreds of declarations.
Be careful about one safety gap. Because aliases are interchangeable, the compiler will not stop you from passing a voltage where a duty cycle is expected if both are plain integers. If you need real type safety, wrap the value in a single-member structure; the compiler then treats each wrapper as a distinct type.
Practice Exercise
Create a header file with typedefs for sample_t (16-bit unsigned), millivolt_t (32-bit signed) and handle_t (pointer to void). Use them in a small program, print sizeof for each, then change sample_t to 32 bits and see that nothing else in the program needs editing.
Summary and Key Takeaways
- typedef adds a new name for an existing type; it creates no new type and no storage.
- One statement may define several names, but it cannot include static or extern.
- typedef beats #define for types because the compiler understands it and scope applies.
- In embedded C, typedef makes code portable, readable and easier to maintain.
Next we apply typedef to the harder cases: pointers, arrays and function types.
Frequently Asked Questions
What does C typedef do?
It gives an existing type another name, so you can use the short or meaningful name wherever the original type is allowed.
Does typedef create a new data type?
No. The alias and the original type are identical and fully interchangeable.
Is typedef better than #define?
For types, yes. The compiler handles scope and declarations of multiple variables correctly, which macros do not.
Can I use static with typedef?
No. Declare the type first, then apply static to the variable.
Does typedef use extra memory or time?
No. It exists only during compilation.
Are uint8_t and uint16_t typedefs?
Yes. They are defined in stdint.h as aliases for suitable built-in integer types on each platform.
Learn C for Embedded Systems, Free
Work through the full free embedded C course with runnable examples, output and interview practice.

1 Comment