- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Typedef in C
C typedef
The C programming language provides a keyword called typedef to set an alternate name to an existing data type. The typedef keyword in C is very useful in assigning a convenient alias to a built-in data type as well as any derived data type such as a struct, a union or a pointer.
Sometimes it becomes clumsy to use a data type with a longer name (such as "struct structname" or "unsigned int") every time a variable is declared. In such cases, we can assign a handy shortcut to make the code more readable.
typedef Syntax
In general, the typedef keyword is used as follows −
typedef existing_type new_type;
typedef Examples
In this chapter, let us have a look at some of the use-cases of typedef.
Example 1
In C language, the keyword "unsigned" is used to declare unsigned integer variables that can store only non-negative values.
C also has a keyword called "short" that declares an integer data type that occupies 2 bytes of memory. If you want to declare a variable that is short and can have only non-negative values, then you can combine both these keywords (unsigned and short):
short unsigned int x;
If there are going to be many variables to be declared of this type, it will not be very convenient to use these three keywords every time. Instead, you can define an alias or a shortcut with the typedef keyword as follows −
typedef short unsigned int USHORT;
This tells the compiler that the identifier USHORT corresponds to "short unsigned int" type. Hereafter, you can use USHORT in the variable declaration statement −
USHORT x;
Example 2
C also has the keyword static to indicate that such a variable is initialized only once. The keyword "long" allocates 8 bytes to store an integer on a 64-bit system. We can declare a variable of this type as follows −
static unsigned long x;
However, we can’t use the keyword "static" in a "typedef" statement, however we can use typedef to assign a shortcut alias to this type of declaration −
typedef signed long SLONG; static SLONG x;
Note: As a convention, the alias names are expressed in uppercase, just to differentiate between the built-in type and the aliases used.
Example 3
The following example demonstrates how you can use alias names in a C program −
#include <stdio.h> int main() { typedef short unsigned int USHORT; typedef signed long int SLONG; static SLONG x = 100; USHORT y = 200; printf("Size of SLONG: %d \nSize of USHORT: %d", sizeof(SLONG), sizeof(USHORT)); return 0; }
Output
When you run this code, it will produce the following ouput −
Size of SLONG: 8 Size of USHORT: 2
Defining a Structure using Typedef
Normally, we need to declare a struct variable by prefixing the name of struct_type in the declaration statement as −
struct struct_type var;
If writing the type name in this manner feels cumbersome, then you can use typedef to assign an alias −
typedef struct struct_type ALIAS;
Example
In this example, we define a structure type and then use the typedef keyword to set an alias for it −
#include <stdio.h> int main() { typedef unsigned long int ULONG; typedef short int SHORT; struct mystruct { ULONG a; SHORT b; }; typedef struct mystruct STR; STR s1 = {10, 20}; printf("%ld %u", s1.a, s1.b); return 0; }
Output
Run the code and check its output −
10 20
There is an alternate approach to use the typedef keyword. We can combine it in the structure definition itself, as given below −
typedef struct mystruct { ULONG a; SHORT b; } STR; STR s1 = {10, 20};
Typedef for Struct Pointer
The typedef keyword may also be used to assign a new identifier to any pointer type. Normally, we declare a pointer variable as follows −
struct mystruct * x;
Instead, we can use the typedef keyword as follows −
typedef struct mystruct { ULONG a; SHORT b; } STR; typedef STR * strptr;
It allows you to declare a pointer of this type in a much more concise way −
strptr ptr;
We can then assign the address of a corresponding struct variable to the pointer.
Example
The following example shows how you can use typedef to create a struct prointer −
#include <stdio.h> int main() { typedef unsigned long int ULONG; typedef short int SHORT; typedef struct mystruct { ULONG a; SHORT b; } STR; STR s1 = {10, 20}; typedef STR * strptr; strptr ptr = &s1; printf("%d %d \n", s1.a, s1.b); printf("%d %d", ptr->a, ptr->b); return 0; }
Output
When you run this code, it will produce the following output −
10 20 10 20
Typedef for Union
We can use the typedef keyword to assign a shortcut alias to any union type.
Example
The following example illustrates how you can use typedef in creating unions −
#include <stdio.h> int main() { typedef unsigned long int ULONG; typedef short int SHORT; typedef union myunion { char a; int b; double c; } UNTYPE; UNTYPE u1; u1.c = 65.50; typedef UNTYPE * UNPTR; UNPTR ptr = &u1; printf("a:%c b: %d c: %lf \n", u1.a, u1.b, u1.c); printf("a:%c b: %d c: %lf \n", ptr->a, ptr->b, ptr->c); return 0; }
Output
Run the code and check its output −
a: b: 0 c: 65.500000 a: b: 0 c: 65.500000
typedef vs #define in C
The typedef keyword is often confused with the #define directive. In C language, #define is a preprocessor directive. It is an effective method to define a constant.
The syntax of using #define is as follows −
#define name value
For example −
#define PI 3.14159265359
The #define statement can also be used to define a macro −
#define SQUARE(x) x*x
A macro works like a function. However, the value is substituted at the preprocessor level when called.
printf("%d", SQUARE(5));
#define is a preprocessor directive, while typedef is evaluated at the time of compilation.
- typedef is limited to giving symbolic names to types only. #define can be used to define alias for values as well. For example, you can define "1" as "ONE".
- typedef interpretation is performed by the compiler, whereas #define statements are processed by the pre-processor.
Example
In the following code, we use both these features (typedef and #define) −
#include <stdio.h> #define MAX 10 int main() { typedef unsigned long int ULONG; typedef short int SHORT; typedef struct employee { char name[MAX]; int age; } EMP; EMP e1 = {"Krishna", 25}; printf("Name: %s \nAge: %d", e1.name, e1.age); return 0; }
Output
When you run this code, it will produce the following output −
Name: Krishna Age: 25