C++ Data Types Reference šŸŽÆ

beginner
17 min

C++ Data Types Reference šŸŽÆ

Welcome to this comprehensive guide on C++ data types! In this lesson, we'll explore the various types available in C++, their uses, and practical examples to help you understand them better. Let's get started!

Understanding Data Types šŸ“

Data types are a way to categorize variables according to the type of data they hold. In C++, we have several built-in data types:

  1. int: Integers
  2. float: Floating-point numbers
  3. double: Double precision floating-point numbers
  4. char: Characters
  5. bool: Boolean (true or false)
  6. string: Strings (not a built-in type, but we'll discuss it anyway)

Integer (int) šŸ’”

An int is used to store whole numbers, both positive and negative. The size of an int varies depending on the system you're working on.

cpp
#include <iostream> int main() { int myNumber = 10; std::cout << "The value of myNumber is: " << myNumber << std::endl; return 0; }

šŸ’” Pro Tip: To print the value of a variable, we use the std::cout function and the stream insertion operator <<.

Floating-point Numbers (float and double) šŸ’”

float and double are used to store real numbers with decimal points. double provides more precision than float.

cpp
#include <iostream> int main() { float myFloat = 3.14f; double myDouble = 3.14159265358979323846; std::cout << "The value of myFloat is: " << myFloat << std::endl; std::cout << "The value of myDouble is: " << myDouble << std::endl; return 0; }

Character (char) šŸ’”

A char is used to store individual characters. In C++, each character is represented by its ASCII value.

cpp
#include <iostream> int main() { char myCharacter = 'A'; std::cout << "The value of myCharacter is: " << myCharacter << std::endl; return 0; }

Boolean (bool) šŸ’”

bool is used to store logical values: true or false.

cpp
#include <iostream> int main() { bool isTrue = true; bool isFalse = false; std::cout << "The value of isTrue is: " << isTrue << std::endl; std::cout << "The value of isFalse is: " << isFalse << std::endl; return 0; }

String (Not Built-In) šŸ’”

Though C++ does not have a built-in string data type, we can still work with strings using the string library.

cpp
#include <string> #include <iostream> int main() { std::string myString = "Hello, World!"; std::cout << "The value of myString is: " << myString << std::endl; return 0; }

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What is the output of the following code?

That's it for this part! In the next lesson, we'll dive deeper into C++ data types and explore more complex concepts. Stay tuned! šŸš€