C++ language is an evolved version of the C language. In development, the most commonly used features and functions in C++ come, at least in part, from the C language. One of these C-based features is the typedef
specifier. While there is a type
alias in C++ typedef
is still a strong feature which allows us to create abbreviations for long types. In this article, we will explain how to use typedef
in C++, there are also examples that can be used by a professional C++ Compiler.
Table of Contents
What Is typedef In C++
The typedef
specifier is used in a declaration of a new type, generally, it is an abbreviation or simplification of an existing type. The typedef
name is used to create an alias term that can be used anywhere in coding to define existing data types, user-defined data types, and pointers. Typedef
allows us to give descriptive names to standard data types, that can be used to help developers, as a self-documenting code. Typedef aliases are very useful when the predefined name is too long or complex to write again and again. The typedef
specifier is generally used at the start of coding or used in headers.
What is the syntax for using typdef in C++?
The typedef syntax is as follows:
1 2 3 |
typedef <known_type> <new_alias>; |
Note that, a typedef
specifier cannot be used in the declaration of a function parameter nor in the decl-specifier-seq of a function definition.
For example, we can create a new ULINT
alias for the long long int
as below,
1 2 3 |
typedef unsigned long int ULINT; |
then, we can use our new alias as below,
1 2 3 |
ULINT x; |
this is same as:
1 2 3 |
unsigned long int x; |
typedef
can be used with struct declarations and other declarations too.
Is there a simple example of how to use typedef in C++?
Here is a simple example that shows how we can use typedef
to create a new alias.
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> typedef unsigned long int ULINT; int main() { ULINT x; // same as : unsigned long int x; } |
Here is another example that shows how we can use typedef
to create a new alias of a struct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> typedef struct my_node { float val; int next; }; int main() { my_node a; // same as: struct my_node a; } |
In modern C++, there is also a type alias. We have more articles about type alias as described below.
What is type alias in C++ is it an alternative to typedef in C++?
Type Alias is a term that refers to a previously defined types, alias declaration can be used to declare a name to use as a synonym for a previously declared type. We use using
declaration (using-declaration) to declare a type alias, and it is effectively the same as typedef
.
If you are looking how to use alias template please use this dynamic search: https://learncplusplus.org/tag/alias-template/. For more information about alias template feature, please see Alias templates Proposal document.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition