Summary - Advanced typedef
- C course
- Intermediate
- Lesson 111/116
- 5 min
Combine struct, fp and opaque types to write a module interface.
typedef summary
In this summary you'll combine typedef enum, typedef struct and typedef function pointer into one program. The program contains a task struct and a function pointer that prints it.
The structure: you define priority levels with a Priority enum. A Task struct carries a name and priority. A TaskPrinter function pointer is assigned to the printing function. main creates a task and prints it through the pointer. This combines the three core uses of typedef in one program: constants with enum, data with struct, behavior abstraction with function pointer.
Practice Write the typedef summary program
Define typedef enum { LOW, MEDIUM, HIGH } Priority;. Define typedef struct { char name[32]; Priority priority; } Task;. Define typedef void (*TaskPrinter)(const Task *);. Write void print_task(const Task *t) using const char *prio[] = {"düşük", "orta", "yüksek"}; and printf("%s [%s]\n", t->name, prio[t->priority]);. In main write Task t = {"Yedekle", HIGH}; TaskPrinter printer = print_task; printer(&t);.
Starter code
#include <stdio.h>
// Define Priority enum with typedef
// Define Task struct with typedef
// Define TaskPrinter function pointer with typedef
// Write print_task function
// Create a Task in main and call via printer
int main(void) {
return 0;
}Sample solution
#include <stdio.h>
typedef enum { LOW, MEDIUM, HIGH } Priority;
typedef struct {
char name[32];
Priority priority;
} Task;
typedef void (*TaskPrinter)(const Task *);
void print_task(const Task *t) {
const char *prio[] = {"düşük", "orta", "yüksek"};
printf("%s [%s]\n", t->name, prio[t->priority]);
}
int main(void) {
Task t = {"Yedekle", HIGH};
TaskPrinter printer = print_task;
printer(&t);
return 0;
}Expected output
Yedekle [yüksek]
Summary
In this group you learned advanced typedef uses. You saw typedef struct, typedef function pointer, typedef array, opaque types and typedef enum. You combined them to use enum, struct and function pointer in one program.
typedef is the core abstraction tool in C. Short type names make code readable; opaque types hide internals. In the next group the intermediate final project awaits; there you'll combine struct, linked list, function pointer and file I/O. Solid typedef skill is an inseparable part of clean C code.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does typedef do?
- Gives a type a short name Correct answer
- Allocates memory
- Sets up a loop
- Opens a file
Explanation: typedef gives a type a new, short name.
2 Match the typedef uses
| Term | Meaning |
|---|---|
typedef struct |
Data structure |
typedef enum |
Named constants |
typedef fp |
Behavior abstraction |
opaque |
Hidden internals |