Data Types in cpp

Introduction to Data Types

Data types refer to the data types used in a program, such as integers, floating-point numbers, characters, etc. At the same time, it may seem like a small detail, but choosing the right data type for a particular task can have a huge impact on the performance and functionality of a program.

In this module, we'll explore the different types of data that can be used in programming and discuss why choosing the right data type is essential. So let's dive into the world of data types!

Basic Data Types

Let's have a very closer look at some of the basic data types that are commonly used in programming:

1. Integer data type (int)

Integers are whole numbers without any decimal points. In C++, the "int" data type represents integers. Integers can be positive or negative and range in value from -2147483648 to 2147483647.

2. Floating-point data types (float, double)

Floating-point data types are used to represent numbers with decimal points. In C++, the "float" and "double" data types represent floating-point numbers. The "float" data type can represent numbers with up to 7 digits of precision, while the "double" data type can represent numbers with up to 15.

3. Character data type (char)

The "char" data type represents single characters, such as letters, numbers, or symbols. In C++, characters are represented using the ASCII encoding system, which assigns a numerical value to each character.

4. Boolean data type (bool)

The "bool" data type represents true/false values. In C++, "true" is represented by the value 1, while "false" is represented by 0. Booleans are commonly used in conditional statements and loops to control the flow of a program.

These basic data types are essential building blocks of any C++ program, and understanding how to use them effectively is crucial for writing efficient and functional code.

Derived Data Types

In addition to the basic data types, several derived data types are commonly used in C++ programming. Let's look at a few of them:

1. Array data type

An array is a collection of the elements of same data type that are stored in the contiguous memory locations. In C++, arrays are declared by specifying the data type of the elements, followed by the name of the array and the number of elements it will contain.

2. String data type

A string is a sequence of the characters that is used to represent text. In C++, strings are represented using the "string" data type, which is a class that provides several useful functions for working with strings.

3. Pointer data type

A pointer is a variable used for storing memory address of another variable. In C++, pointers are represented using the "*" symbol. Pointers are commonly used for dynamic memory allocation and for passing variables to functions by reference.

4. Structure data type

A structure is a collection of the related data stored as a single unit. In C++, structures can be defined using the "struct" keyword and contain various data types.

These derived data types provide additional functionality and flexibility in various programming tasks. By understanding how to use them effectively, you can take your C++ programming skills to the next level.

User-Defined Data Types

In addition to basic and derived data types, C++ allows users to define their own data types using two methods: enumerated and typedef data types.

1. Enumerated data type

An enumerated data type, also known as an enum, is a user-defined data type which consists of a set of named constants. Enumerated data types are commonly used in C++ programs to represent a limited set of values. For example, you may use an enum to represent the days of the week or the months of the year.

2. Typedef data type

A typedef is a C++ keyword that allows you to create a new name for an existing data type. This can be useful for developing shorter, more readable names for complex data types or creating platform-independent code that can be easily ported to different systems. For example, you might use a typedef to create a new name for a complex data structure used throughout your program.

These user-defined data types provide a way to make the code even more readable and easier to maintain by creating meaningful names for data types that might otherwise be difficult to understand. By using these techniques effectively, you can make more robust, flexible C++ programs easier to work with over time.

Data Types and Their Ranges

Data Type
Size (in bytes)
Range
bool1true or false
char1-128 to 127 or 0 to 255 (unsigned)
short2-32,768 to 32,767 or 0 to 65,535 (unsigned)
int4-2,147,483,648 to 2,147,483,647 or 0 to 4,429,967,295 (unsigned)
long4 or 8-2,147,483,648 to 2,147,483,647 or 0 to 4,429,967,295 (unsigned)
long long8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (or 0 to 18,446,744,073,709,551,615 unsigned)
float41.17549e-38 to 3.40282e+38
double82.22507e-308 to 1.79769e+308
long double163.3621e-4932 to 1.18973e+4932

Learn via Video Course

C++ Programming (English) Logo

C++ Programming (English)

2056

18 hrs