Home » C++ » C++ Variables: Understanding the Different Data Types

C++ Variables: Understanding the Different Data Types

In C++, a variable is a named storage location that can hold a value of a specific data type. There are several basic data types in C++, including:

  1. int: This data type is used to store integers (whole numbers) such as -1, 0, and 1.
int x = 5;
  1. float: This data type is used to store decimal numbers with single precision.
float y = 3.14;
  1. double: This data type is used to store decimal numbers with double precision.
double z = 3.14159265358979323846;
  1. char: This data type is used to store individual characters, such as ‘a’, ‘b’, ‘c’, etc.
char letter = 'A';
  1. bool: This data type is used to store true or false values.
bool isTrue = true;
  1. string: This data type is used to store strings of characters.
string name = "John";
  1. Array: This data type is used to store a collection of elements of the same data type.
int myArray[5] = {1, 2, 3, 4, 5};
  1. Pointers: This data type is used to store memory addresses, and allows you to manipulate memory directly.
int x = 5;
int *p = &x;
  1. Enumeration: This data type is used to define a set of named integer constants.
enum Color { RED, GREEN, BLUE };
  1. Structs: This data type is used to group together multiple variables of different data types into one unit.
struct Person {
   string name;
   int age;
   char gender;
};

Leave a Comment

Your email address will not be published. Required fields are marked *