• Home
  • 1.10 Type Aliases (typedef)

1.10 Type Aliases (typedef)

View Categories

1.10 Type Aliases (typedef)

< 1 min read

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 alias uint for the existing type unsigned int.
  • typedef does not create a new data type. It only introduces an alternate name for an existing type.
  • uint student_count = 120; is equivalent to writing unsigned int student_count = 120;.
  • Type aliases are commonly used to improve code readability and simplify lengthy type declarations.
  • Modern C++ provides the using keyword as an alternative to typedef, especially for template aliases.
  • std::cout << "Student Count: " << student_count << '\n'; prints the value stored in the variable.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *