Data Types in C++

In C++, data types define the kind of data a variable can hold and the operations that can be performed on it. C++ supports a variety of data types, including primitive types and composite types.

Primitive data types

Primitive data types are the basic building blocks of C++ programs. They represent simple data types such as numbers, characters, and booleans.

Composite data types

Composite data types are more complex than primitive data types. They can be used to represent more complex data structures, such as arrays, strings, and objects.

Here are some common data types in C++:

Integer Types

  1. int: Represents signed integers, e.g., -1, 0, 42.
  2. short: Represents short integers with a smaller range.
  3. long: Represents long integers with a larger range.
  4. long long: Represents very long integers (C++11 and later).
int age = 30;

Floating-Point Types

  1. float: Represents single-precision floating-point numbers, e.g., 3.141.
  2. double: Represents double-precision floating-point numbers, e.g., 2.718281828.
  3. long double: Represents extended-precision floating-point numbers.
double pi = 3.14159265359;

Character Types

  1. char: Represents a single character, e.g., 'A', '5'.
  2. wchar_t: Represents wide characters for internationalization (Unicode).
char grade = 'A';

Boolean Type

  1. bool: Represents boolean values, either true or false.
bool isCplusplusFun = true;

Enumeration Types

  1. enum: Represents a user-defined set of named integer constants.
enum Color { Red, Green, Blue }; Color selectedColor = Green;

Custom User-Defined Types:

  1. struct: Defines a structure to group related data elements.
  2. class: Defines a class for object-oriented programming with data and functions.
  3. union: Defines a union, which is similar to a struct but uses the same memory location for all its members.
struct Point { int x; int y; }; class Circle { private: double radius; public: Circle(double r) : radius(r) {} double area() { return 3.141 * radius * radius; } };

Void Type

  1. void: Represents an empty or undefined data type and is often used for functions that do not return a value.
void printMessage() { std::cout << "Hello, C++!" << std::endl; }

Pointer Types

  1. int*, double*, char*: Represent pointers to variables of other types.
int* agePtr = &age;


Data types in C++ are used to specify the kind of data a variable can hold and the operations that can be performed on it. Common data types include integers (int), floating-point numbers (double), characters (char), booleans (bool), user-defined types (e.g., struct and class), and more. Choosing the appropriate data type is essential for efficient memory usage and accurate representation of data in C++ programs.