Data Types | C++

 Data types in C++ are used to define the kind of data that a variable can store. There are two types of data types in C++: primitive and derived.

Primitive data types are the basic building blocks of C++. They are predefined by the language and cannot be modified. The following are the primitive data types in C++:

  • Integer: Stores whole numbers, without decimals.
  • Floating-point: Stores fractional numbers, with decimals.
  • Character: Stores a single character.
  • Boolean: Stores a logical value, either true or false.
  • Void: Stores no value.

Derived data types are created from primitive data types. They are more complex than primitive data types and can store more information. The following are some of the derived data types in C++:

  • String: Stores a sequence of characters.
  • Array: Stores a collection of elements of the same type.
  • Structure: Stores a collection of related data items.
  • Class: Stores a collection of related data items and functions.

Data types are important in C++ because they determine how the compiler interprets the code. When you declare a variable, you must specify the data type that the variable will store. This tells the compiler how much memory to allocate for the variable and how to interpret the data that is stored in the variable.

For example, the following code declares two variables, one of type int and one of type char:


int my_integer = 10; char my_character = 'A';



all the data types in C++ with their definitions and examples:

Primitive data types

  • Integer: Stores whole numbers, without decimals.
C++
int my_integer = 10;
  • Floating-point: Stores fractional numbers, with decimals.
C++
float my_float = 1.23;
double my_double = 1.23456789;
  • Character: Stores a single character.
C++
char my_character = 'A';
  • Boolean: Stores a logical value, either true or false.
C++
bool my_boolean = true;
  • Void: Stores no value.
C++
void my_void_function() {}

Derived data types

  • String: Stores a sequence of characters.
C++
string my_string = "Hello, world!";
  • Array: Stores a collection of elements of the same type.
C++
int my_array[10];
  • Structure: Stores a collection of related data items.
C++
struct my_struct {
  int my_integer;
  float my_float;
  char my_character;
};
  • Class: Stores a collection of related data items and functions.
C++

class my_class {
public:
  int my_integer;
  float my_float;
  char my_character;

  void my_function() {}
};

Explore more :---------------->

Define sorting | AsgarTech

Data Types in C++ | AsgarTech




No comments: