Site icon Learn C++

What is _t (underscore-t) in C++? Why some types followed by _t?

What is t underscore t in C++ Why some types followed by t

The C++ Programming Language is huge, it may have millions and millions of keywords, and commands with every new library or feature addition. In programming, it is hard to memorize all of these keywords. Sometimes when we are using some keywords, we may have some questions about them, because we want to learn and memorize them logically. If you are using C or C++, we may have these questions about some type declarations, i.e. wchar_ttime_tsize_t, etc. Why do they have _t (underscore-t) at the end of them? What does a type followed by a _t mean in C++? In this post, we explain why some types are followed by _t and some are not.

What is type in C++? What is a type specifier?

In C and C++, types determine how much memory is allocated to an object and how the program interprets the bit patterns found in the object’s storage allocation. A data type is the set of values, often implementation-dependent, identifiers can assume, together with the set of operations allowed on those values. Some of the type keywords are, charwchar_tchar16_tchar32_t, floatdouble, intenum, structclass, and union.

We can use these data types with some prefixes such as signed, unsigned, short, longlong long.These are called qualifiers, type specifiers, and are applied to the basic types of C++. The type specifier with one or more optional modifiers is used to specify the type of the declared identifier: If you want to learn more about data types here is a good post.

What is _t (underscore-t) in C++?

In C++, some types followed by _t, the _t (underscore-t) addition to some type names implies a typedef, a defined data type. This typedef is based on an existing type. For example, to define a size type, they used size_t typedef which is read as ‘size type’. This new typedef allows developers to develop applications with compatibility and consistency between architectures, between different compiler versions or different standards, or in different compiler options these types give us flexibility.

Why are some types followed by _t some are not in C++?

In general, basic types in C++ have no _t additions, these are char, int, float, double, etc. Remember that for some types the “defined data type” _t addition is used in their names. Examples of these are wchar_t, char16_t, char32_t, clock_t, time_t, but there are others.

For example, char is used for representing characters from the ASCII character set. wchar_t is used for new so called Wide, or Unicode, type which use the UTF-16 character set.

wchar_t represents distinct codes for any element of the largest extended character set in any of the supported locales.

[crayon-676413526adb1450719895/]

char16_t is a fundamental data type that can represent a 16-bit character type.

[crayon-676413526adbe229186574/]

In another example, the int32_t family of types was added in the C99 standard, so they used the reserved names to avoid conflict with already existing software.

clock_t defines the data type returned by the clock function:

[crayon-676413526adc0126230349/]

Another example is the time_t data type which is used to declare a time value. This value was a 16-bit integer, before year 2000. It became a 32-bit integer to deal with the Y2K problems in dates. In the future this type may be 64-bit integer, may be for Y36/Y38 problems (likely not an issue for any of reading this right now), or for calculations that extend far into humankind’s future. This time_t data type allows you code whatever the type of the compiler.

time_t defines the value used by the time functions declared in time.h

[crayon-676413526adc1384717236/]

Can I use _t (underscore-t) in my own types in C++?

You can use typedef in your own data types. In general, code should be understandable, so I recommend you use standard keywords. Be sure that is your own datatype and defined properly.

[crayon-676413526adc7931691031/]

If you want your code to be friendly and future proof, avoid using your own types as much as possible. For a new developer trying to understand hundreds of lines of C++ code and the logic behind them, these things (like myINT_t above) can make it hard to understand what was is going on. Thus, the example below is much more friendly for every developer, not just the original coder:

[crayon-676413526adc8337859954/]

Is there an example of types with _t (underscore-t) in C++?

Here is an example that uses different defined char types:

[crayon-676413526adc9247931804/]

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.

Exit mobile version