CC++C++11C++14C++17Introduction to C++Learn C++

What is type casting in C programming?

What is type casting in C programming

C and C++ are one of the top 3 programming languages worldwide. Type casting is one of the most used methods to convert similar variable types to different types in C and C++. Type casting in C or in C++ can be done by using data type in parenthesis in the code lines in a C++ Editor like C++ Builder or Dev-C++. In this post, we explain type casting in C and other casting methods available to us in C++.

What is implicit type casting (standard conversion) in C programming?

Implicit type casting is an automatic casting that copies a variable to another compatible variable type, this is also known as a standard conversion. This method does not require any operator, it is automatically performed when a value is copied to a compatible type. Implicit type casting affects fundamental data types and allows conversions such as the conversions between numerical types, to or from bool, and some pointer conversions. For example, we can copy an unsigned char value to an unsigned int value as below,

Here, the value of i has been promoted from unsigned char to unsigned int and there is no need to specify any type-casting operator. This will output integer value as 221 as same as value of c variable.

Can you show me some wrong examples of type casting in C?

In implicit type casting developer should be careful about variable types. Some of these conversions may imply a loss of precision, which the editor or compiler can signal with a warning. For example, in a char to int conversion, char c will be -35 which comes from 221-256 = -35,

Here, the value of i has been promoted from short to int and we have not had to specify any type-casting operator. and the output of the implicit type value of the integer will be -35. Generally, because of these kinds of conversion problems, if you are not sure about types and their ranges, we do not recommend these kinds of type castings. Note that, this warning can be avoided with an explicit conversion.

In another implicit type conversion, higher bit types to lower bit type value conversions may cause a loss of value or wrong values as a result. For example, we have an integer value of 300 and if we convert to char as below

This will result the value 44 which comes from 300-256 = 44. If we use the keyword unsigned in front of char then this problem is avoided.

What is explicit type casting in C programming?

C and C++ is powerful decent programming languages. Generally, most of the conversions, especially those that imply a different interpretation of the value, require an explicit conversion.

Here are two different notations,

  • Explicit C-Like Type Casting
  • Explicit Functional Type Casting

In C-Like explicit type casting, just add type name / data type in parenthesis then expression.

Here is the Syntax of Explicit C-Like Type Casting:

In explicit functional casting, just add type name / data type then expression in parenthesis.

Here is the Syntax of Explicit Functional Type Casting:

Here is a C example to both of these syntaxes:

In explicit type casting, developer should be careful about variable types too, as given examples above. Some of these conversions may imply a loss of precision without a warning.

What is type casting in C++ programming?

In C++, casting is a technique to convert one data type to another data type. Implicit type casting, explicit C-Like type casting, and explicit functional type casting can be used in C++. In addition to these, there are 4 more casting operators that can be used in casting data types. The operator used for this purpose is known as the cast operator. It is a unary operator which forces one data type to be converted into another data type.

In C++, a cast operator is a Unary Operator which forces one data type to be converted into another data type.
In general, C++ supports four types of casting:

  1. Static Cast (static_cast)
  2. Dynamic Cast (dynamic_cast)
  3. Constant Cast (const_cast)
  4. Reinterpret Cast (reinterpret_cast)

Although the free C++ Builder Community Edition is extremely powerful it is intended for students, beginners, and startups. If you are a regular business or do not qualify for the free community edition, then you can download a free trial of the very latest full RAD Studio C++ Builder version.

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?