C++ Data Types

In C++, a data type is a classification of data that defines the possible values and operations that can be performed on the data. C++ provides several built-in data types that can be used to store different types of data such as numbers, characters, and boolean values.

Integer Data Types

The integer data types in C++ are used to store whole numbers. There are several integer data types in C++, including short, int, and long. The short data type is used to store small integers, the int data type is used to store medium-sized integers, and the long data type is used to store large integers. The size of these data types can vary depending on the compiler and the system architecture.

short a = 10; 
int b = 20; 
long c = 30;
cout<<"Size of short: "<<sizeof(a)<<" bytes"<<endl;
cout<<"Size of int: "<<sizeof(b)<<" bytes"<<endl;
cout<<"Size of long: "<<sizeof(c)<<" bytes"<<endl;

Floating-Point Data Types

The floating-point data types in C++ are used to store decimal numbers. The two most commonly used floating-point data types are float and double. The float data type can store single-precision floating-point numbers, and the double data type can store double-precision floating-point numbers. The size of these data types can also vary depending on the compiler and the system architecture.

float d = 3.14;
double e = 3.14159;
cout<<"Size of float: "<<sizeof(d)<<" bytes"<<endl;
cout<<"Size of double: "<<sizeof(e)<<" bytes"<<endl;

Character Data Type

The character data type in C++ is used to store a single character, such as a letter or a symbol. The character data type is represented by the char keyword. In C++, a character is stored as a number, known as its ASCII value. The size of the char data type is 1 byte.

char f = 'a';
cout<<"Size of char: "<<sizeof(f)<<" bytes"<<endl;

Boolean Data Type

The boolean data type in C++ is used to store true or false values. The boolean data type is represented by the bool keyword. The size of the bool data type is 1 byte.

bool g = true;
cout<<"Size of boolean: "<<sizeof(g)<<" bytes"<<endl;

String Data Type

In C++, string is not a built-in data type, instead, it is a class defined in the standard template library (STL). String class provides a lot of inbuilt functions that make string manipulation very easy. String class objects can be used much like a char array.

#include 
#include 
using namespace std;

int main() {
string myString = "Hello, World!";
cout << "The string is: " << myString << endl;
cout << "The length of the string is: " << myString.length() << endl;
return 0;
}

In this example, we created a string object called myString and assigned it the value “Hello, World!”. We then used the cout statement to display the value of the string and the length() function to display the number of characters in the string. The output of the above program will be: The string is: Hello, World! and The length of the string is: 13.

Conclusion

C++ offers a wide range of data types, from the basic built-in types like int, float, and char, to more advanced types like string and classes. Understanding the different data types and their characteristics is crucial for designing efficient and effective C++ programs. String class is a very useful data type in C++ which is widely used to store and manipulate strings. It is important to choose the right data type for a particular task and use it appropriately.

Leave a Reply

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