♦ C++ Casting
♣ About C++ Casting
Casting in C++ means changing the Data Type of a variable.Not all Data Types can be changed to all Data Types.
Only a few Data Types can be changed from one to another.
For example, an integer can be changed to float and vice-versa.
Below are represented various types of Data Type conversions.
♣ Example1: Float to Int in C++
Float is a Data Type which can store a real number of certain range.Int is a Data Type which can store integer values of certain range.
Float to Int conversion will chop down the decimal-part(if exists) of the number to give out integer.
In this example, multiple Float to Int conversions are illustrated.
Example
Input:
Output:
♣ Example2: Int to Float in C++
Int is a Data Type which can store integer values of certain range.Float is a Data Type which can store a real number of certain range.
Int to Float conversion will add an extra decimal option for the variable which can be used potentially in future for float type operations.
In this example, multiple Int to Float conversions are illustrated.
Example
Input:
Output:
♣ Example3: Int to String in C++
Int is a Data Type which can store integer values of certain range.A string is a Data Type which composes of a sequential arrangement of characters forming a Text.
All the type of Integer can be converted to String since Integers are a special form of characters.
In this example, multiple Int to String conversions are illustrated.
Example
Input:
Output:
♣ Example4: String to Int in C++
A string is a Data Type which composes of a sequential arrangement of characters forming a Text.Int is a Data Type which can store integer values of certain range.
Note that only those strings can be converted to an integer which is numeric, for example, "45", "2.3", etc.
In this example, multiple String to Int conversions are illustrated.
Example
Input:
Output:
♣ Example5: Float to String in C++
In this example, multiple Float to String conversions are illustrated. Example
Input:
Output:
♣ Example6: String to Float in C++
In this example, multiple String to Float conversions are illustrated. Example
Input:
Output: