Typedef

Dev Frank
4 min readMay 22, 2024

--

Typedef is a keyword used to create an alias for an existing data type. It simply means typedef declaration allows you to create custom type names that can replace type specifiers like int , float, and double. It does not allocate any storage. This can improve code readability, manageability, and portability.

Think of typedef like giving a nickname to a friend. Instead of calling them by their full name every time, you can use a nickname. Similarly, typedef in programming lets you create a shortcut name for a data type. So, instead of saying “unsigned long int” every time, you can just use your shortcut name, like “ULong”

Basic Syntax

The basic syntax for using typedef is

typedef existing_type new_name;

Example Usage

Simple Types

You can use typedef to create an alias for a simple data type:

typedef unsigned long ulong;

ulong a, b; // a and b are of type unsigned long

In this example, ulong is an alias for unsigned long, making the code easier to read and write.

Let’s take a more complex example

#include <stdio.h>
#include <string.h>

// Define a new type alias 'Byte' for 'unsigned char'
typedef unsigned char Byte;

// Define a structure for a 2D point and create an alias 'Point' for it
typedef struct {
int x;
int y;
} Point;

// Define a structure for a Book and create an alias 'Book' for it
typedef struct {
char title[50];
char author[50];
char subject[100];
int story_id;
} Story;

// Define a function pointer type alias 'Operation' for a function taking two ints and returning an int
typedef int (*Operation)(int, int);

// A sample function to add two integers
int add(int a, int b) {
return a + b;
}

int main() {
// Using the 'Byte' alias
Byte s = 255;
printf("Value of Byte : %u\n", s);

// Using the 'Point' alias
Point p1 = {30, 50};
printf("Point p1: (%d, %d)\n", p1.x, p1.y);

// Using the 'Book' alias
Story story;
strcpy(story.title, "C Programming");
strcpy(story.author, "Dev Frank");
strcpy(story.subject, "Typedef");
story.story= 70971141;

printf("Story title: %s\n", story.title);
printf("Story author: %s\n", story.author);
printf("Story subject: %s\n", story.subject);
printf("StoryID: %d\n", story.story_id);

// Using the 'Operation' alias for function pointers
Operation op = add;
int result = op(4, 6);
printf("Result of addition: %d\n", result);

return 0;
}

OUTPUT

Value of Byte : 255
Point p1: (30, 50)
Book title: C Programming
Book author: Dev Frank
Book subject: Typedef
Book ID: 70971141
Result of addition: 10

This C program demonstrates typedef to create type aliases (Byte, Point, Book, Operation). It initializes and prints a byte value, a 2D point, a book’s details, and uses a function pointer to add two integer

Function Pointers

Function pointers can be complex, and typedef can simplify their declarations:

typedef int (*Operation)(int, int);

int add(int a, int b) {
return a + b;
}

int subtract(int a, int b) {
return a - b;
}

Operation op;

op = add;
int result = op(5, 3); // result is 8

op = subtract;
result = op(5, 3); // result is 2

Here, Operation is an alias for a pointer to a function that takes two int arguments and returns an int.

Arrays

You can use typedef to create an alias for array types:

typedef int IntArray[10];

IntArray numbers; // numbers is an array of 10 integers
for (int i = 0; i < 10; ++i) {
numbers[i] = i;
}

This can simplify the declaration and usage of arrays.

Structures

Using typedef with structures in C can greatly simplify code by eliminating the need to repeatedly use the struct keyword and by making the code more readable. Here’s a detailed guide on how to use typedef with structures properly:

Without typedef, you need to use the struct keyword each time you declare a variable of the structure type:

struct Point {
int x;
int y;
};

struct Point p1, p2;

With typedef, you can create an alias for the structure, making declarations simpler:

typedef struct Point {
int x;
int y;
} Point;

Point p1, p2;

Combining typedef with Structure Definition

You can define a structure and create a typedef for it in one step:

typedef struct {
int x;
int y;
} Point;

Point p1, p2;

In this example, Point is now an alias for struct { int x; int y; }, and you don’t need to use the struct keyword when declaring variables of this type.

Struct with Typedef in One Line

typedef struct {
int width;
int height;
} Rectangle;

Rectangle rect1, rect2;

Here, Rectangle is an alias for the unnamed structure. Variables rect1 and rect2 are declared as Rectangle.

Struct with Pointer Typedef

It’s common to use typedef to create an alias for a pointer to a structure, which can make code involving pointers more readable:

typedef struct Node {
int data;
struct Node *next;
} Node, *NodePtr;

NodePtr head = NULL;
Node node;
node.data = 10;
node.next = NULL;
head = &node;

Here, Node is an alias for struct Node, and NodePtr is an alias for struct Node*.

Struct within Struct

You can also use typedef with nested structures:

typedef struct {
int x;
int y;
} Point;

typedef struct {
Point topLeft;
Point bottomRight;
} Rectangle;

Rectangle rect;
rect.topLeft.x = 0;
rect.topLeft.y = 0;
rect.bottomRight.x = 10;
rect.bottomRight.y = 10;

In this example, Rectangle contains two Point structures, and you can access their members directly.

Using typedef with structures in C helps to simplify and clarify code by reducing the need to repeatedly write struct and by providing more descriptive type names. This practice is especially useful in large projects and when defining complex structures or opaque types for libraries.

Explore typedef further to level up your coding skills! Dive into advanced topics like typedef with function pointers, opaque types, and nested structures. Practice using typedef in your code to write cleaner, more organized programs. Keep learning and experimenting to become a typedef pro :)!

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.