Table of Contents
A type alias provides an alternate name for an existing data type. The typedef keyword improves readability by replacing long or complex type declarations with shorter, meaningful names.
This example demonstrates how to create and use a type alias with typedef.
Source Code #
#include <iostream>
typedef unsigned int uint;
int main()
{
uint student_count = 120;
std::cout << "Student Count: " << student_count << '\n';
return 0;
}
Output #
Student Count: 120
Explanation #
typedef unsigned int uint;creates the aliasuintfor the existing typeunsigned int.typedefdoes not create a new data type. It only introduces an alternate name for an existing type.uint student_count = 120;is equivalent to writingunsigned int student_count = 120;.- Type aliases are commonly used to improve code readability and simplify lengthy type declarations.
- Modern C++ provides the
usingkeyword as an alternative totypedef, especially for template aliases. std::cout << "Student Count: " << student_count << '\n';prints the value stored in the variable.return 0;terminates the program successfully.