C Notes
Complete guide to typedef in C programming — creating type aliases, typedef with structures, typedef with pointers and arrays, function pointer typedefs, improving code readability, practical examples.
Typing struct Student every time you declare a variable gets tedious. And complex declarations like int (*)(int, int) for function pointers are nearly unreadable. The typedef keyword lets you create aliases — shorter, more meaningful names for existing types. It doesn't create new types; it just gives existing types friendlier names.
Basic typedef Syntax
typedef existing_type new_name;typedef unsigned long int uint64;
typedef char* String;
uint64 population = 8000000000;
String greeting = "Hello, World!";typedef with Structures
This is the most common use of typedef — eliminating the struct keyword from declarations:
Roll Name GPA 101 Alice 3.9 102 Bob 3.7
typedef Patterns for Structures
// Pattern 1: typedef with anonymous struct
typedef struct {
int x, y;
} Point;
// Pattern 2: typedef with named struct (needed for self-reference)
typedef struct Node {
int data;
struct Node *next; // Must use "struct Node" here, not "Node"
} Node;
// Pattern 3: Separate declaration and typedef
struct LinkedList {
Node *head;
int size;
};
typedef struct LinkedList LinkedList;typedef with Pointers
#include <stdio.h>
#include <stdlib.h>
typedef int* IntPtr;
int main() {
IntPtr a, b; // BOTH are int* (unlike: int *a, b which makes b an int)
a = (int *)malloc(sizeof(int));
b = (int *)malloc(sizeof(int));
*a = 42;
*b = 99;
printf("*a = %d, *b = %d\n", *a, *b);
free(a);
free(b);
return 0;
}*a = 42, *b = 99
Warning with pointer typedefs: IntPtr a, b; makes BOTH pointers. Without typedef, int *a, b; makes only a a pointer and b an int.
typedef with Arrays
Student: Charlie Identity matrix[1][1] = 1
typedef with Function Pointers
This is where typedef truly shines — turning unreadable declarations into clean ones:
add(10, 3) = 13 sub(10, 3) = 7 mul(10, 3) = 30
typedef vs #define
| Feature | typedef | #define |
|---|---|---|
| Type | Creates type alias | Text substitution |
| Scope | Follows C scope rules | Global until #undef |
| Pointers | typedef int* P; P a,b; → both pointers | #define P int*; P a,b; → only a is pointer |
| Debugging | Type visible in debugger | Invisible (substituted away) |
| Processed by | Compiler | Preprocessor |
| Can alias | Types only | Anything |
// DANGEROUS with #define:
#define INT_PTR int*
INT_PTR a, b; // Expands to: int* a, b; → a is pointer, b is NOT!
// SAFE with typedef:
typedef int* IntPtr;
IntPtr a, b; // Both are int*Practical Example: Callback System
[CLICK] Button1 at position 150 [KEY] KeyDown with code 65
Platform-Independent Types with typedef
// Common pattern in system headers:
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
typedef int int32_t;
typedef long long int64_t;
// These are already in <stdint.h> (C99)Interview Questions
Q1: Does typedef create a new data type?
No. typedef creates an alias (another name) for an existing type. The compiler treats the alias and the original type as identical — they're interchangeable.
Q2: Why is typedef better than #define for type aliases?
typedef is processed by the compiler (not preprocessor), respects scope, handles pointer declarations correctly (typedef int* P; P a,b; makes both pointers), and is visible in debuggers. #define is simple text substitution that can cause subtle bugs with pointers.
Q3: Can you typedef a typedef?
Yes. typedef int Int32; typedef Int32 MyInt; is perfectly valid. MyInt is now an alias for int.
Q4: Why do self-referential structures need a named struct tag?
In typedef struct Node { int data; struct Node *next; } Node;, the tag Node after struct is needed because at the point where struct Node *next is written, the typedef hasn't been completed yet. You need the tag to refer to the incomplete type.
Summary
typedefcreates readable aliases for existing types- Most commonly used with structures to avoid writing
structeverywhere - Essential for readable function pointer declarations
- Handles pointer declarations correctly (unlike #define)
- Respects scope and is processed by the compiler
- Doesn't create new types — just friendly names for existing ones
- Use with
stdint.htypes for portable, fixed-width integers
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for typedef in C – Creating Type Aliases.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, structures, and, unions, typedef, typedef in c – creating type aliases
Related C Programming Topics