Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Explizite Typumwandlung | Andere Datentypen und Konzepte
C++ Datentypen

bookExplizite Typumwandlung

If you are dealing with variables of type int, rather than just numbers in code, you need to convert the value of a variable to double or float. This can be accomplished using the following syntax:

main.cpp

main.cpp

copy
12345678910
#include <iostream> int main() { int num1 = 9; int num2 = 4; std::cout << "No conversion: " << num1 / num2 << std::endl; std::cout << "With conversion: " << (double)num1 / num2 << std::endl; }

There are several ways to explicitly perform type conversion. This one is called C-style conversion:

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { float num = 6.5; std::cout << (int)num << std::endl; // 6.5 to `int` is 6 std::cout << (bool)num << std::endl; // 6.5 to `bool` is true // Or std::cout << int(num) << std::endl; // 6.5 to `int` is 6 std::cout << bool(num) << std::endl; // 6.5 to `bool` is true }

While this approach is concise, it does not clearly indicate the intent or nature of the conversion, which can lead to ambiguity and potential misuse.

There are more explicit and safer casting operators to provide better control over type conversions.

Most of the time, you only need a static_conversion. We will not discuss other casts in-depth. Here is the syntax:

main.cpp

main.cpp

copy
12345678
#include <iostream> int main() { float num = 6.5; std::cout << static_cast<int>(num) << std::endl; // 6.5 to `int` is 6 std::cout << static_cast<bool>(num) << std::endl; // 6.5 to `bool` is true }

We will not cover the difference between all of those. In general, static_cast is preferable since it is more restrictive and can prevent some unexpected behavior with complex data types.

question mark

Select all the correct ways of performing type conversion.

Select all correct answers

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 7

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookExplizite Typumwandlung

Swipe um das Menü anzuzeigen

If you are dealing with variables of type int, rather than just numbers in code, you need to convert the value of a variable to double or float. This can be accomplished using the following syntax:

main.cpp

main.cpp

copy
12345678910
#include <iostream> int main() { int num1 = 9; int num2 = 4; std::cout << "No conversion: " << num1 / num2 << std::endl; std::cout << "With conversion: " << (double)num1 / num2 << std::endl; }

There are several ways to explicitly perform type conversion. This one is called C-style conversion:

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { float num = 6.5; std::cout << (int)num << std::endl; // 6.5 to `int` is 6 std::cout << (bool)num << std::endl; // 6.5 to `bool` is true // Or std::cout << int(num) << std::endl; // 6.5 to `int` is 6 std::cout << bool(num) << std::endl; // 6.5 to `bool` is true }

While this approach is concise, it does not clearly indicate the intent or nature of the conversion, which can lead to ambiguity and potential misuse.

There are more explicit and safer casting operators to provide better control over type conversions.

Most of the time, you only need a static_conversion. We will not discuss other casts in-depth. Here is the syntax:

main.cpp

main.cpp

copy
12345678
#include <iostream> int main() { float num = 6.5; std::cout << static_cast<int>(num) << std::endl; // 6.5 to `int` is 6 std::cout << static_cast<bool>(num) << std::endl; // 6.5 to `bool` is true }

We will not cover the difference between all of those. In general, static_cast is preferable since it is more restrictive and can prevent some unexpected behavior with complex data types.

question mark

Select all the correct ways of performing type conversion.

Select all correct answers

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 7
some-alt