← Back to C Course | Chapter 12: Advanced Topics | Lesson 1 of 20

C typedef

typedef gives a type a new, shorter or clearer name, like a nickname for a long title. It makes code easier to read.
Syntax
c
typedef existing_type new_name;

new_name variable_name = value;

What is typedef?

typedef unsigned long ulong; lets you write ulong instead of the more verbose unsigned long everywhere in your code, which becomes especially valuable for long, unwieldy type names involving pointers or function signatures.

Example: What is typedef?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Create a type alias with typedef
typedef unsigned long ulong;
// Program execution starts in main()
int main() {
	ulong x = 100;
	// Print formatted text to the screen
	printf("%lu", x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Aliasing Standard Types

For example, typedef int Age; doesn't create a genuinely distinct type the way some languages' type systems would -- Age is fully interchangeable with int, but naming it this way documents the variable's intended meaning at a glance.

Example: Aliasing Standard Types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Create a type alias with typedef
typedef int Age;
// Program execution starts in main()
int main() {
	Age myAge = 25;
	// Print formatted text to the screen
	printf("%d", myAge);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Aliasing Structures

typedef struct {int x; int y;} Point; lets you later declare variables with just Point p; instead of the more verbose struct Point p; -- this shorthand is extremely common in real-world C codebases and libraries.

Example: Aliasing Structures

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Create a type alias with typedef
typedef struct {int x; int y;} Point;
// Program execution starts in main()
int main() {
	Point p = {3, 4};
	// Print formatted text to the screen
	printf("%d %d", p.x, p.y);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Aliasing Pointers

typedef int* IntPtr; lets you write IntPtr values[10]; instead of int* values[10];, though this particular use is somewhat controversial since it can obscure that a variable is actually a pointer, making code harder to read for some.

Example: Aliasing Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Create a type alias with typedef
typedef int* IntPtr;
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	IntPtr ptr = &x;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Best Practices for typedef

Following this convention (like typedef struct {...} Point;) makes it immediately visually clear in code that Point is a typedef'd type rather than a built-in one, helping readers distinguish your custom types from standard ones like int or char at a glance.

Example: Best Practices for typedef

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Create a type alias with typedef
typedef struct {int x; int y;} Point;
// Program execution starts in main()
int main() {
	Point p = {1, 2};
	// Print formatted text to the screen
	printf("%d %d", p.x, p.y);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the semicolon at the end of a typedef declaration.
  2. Writing struct before an alias name that was already defined by typedef.
  3. Assuming a typedef creates a new distinct type, when it is only another name for the same type.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.