C typedef
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting the semicolon at the end of a
typedefdeclaration. - Writing
structbefore an alias name that was already defined bytypedef. - Assuming a typedef creates a new distinct type, when it is only another name for the same type.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions