Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a data type in C++?
3.
Types of Data Types in C++
4.
Primitive Data Types in C++
4.1.
1. Integer 
4.2.
2. Character 
4.3.
3. Boolean 
4.4.
4. Floating Point  
4.5.
5. Double Floating Point 
4.6.
6. Void or Valueless
4.7.
7. Wide Character
5.
Derived Data Types in C++
5.1.
1. Function 
5.2.
2. Array 
5.3.
3. Pointers
5.4.
4. References 
6.
Abstract or User-Defined Data Types in C++
6.1.
1. Class
6.2.
2. Structure
6.3.
3. Union
6.4.
4. Enumeration 
7.
Datatype Modifiers in C++
8.
Frequently Asked Questions
8.1.
What are the derived data types in C++?
8.2.
What are the 3 data types in C++?
8.3.
Is string a data type in C++? 
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

C++ Data Types

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Writing a program in any programming language; we use variables to store information of different types. The information we store can be a number, string, etc. Variables are used to reserve memory locations to store values. When declaring a variable, we need to specify its data type to restrict the type of data to be stored. 

data types in c++

In other words, it can be said that a data type dictates the variable what type of data it can store. Every data type requires some amount of memory which is different for different data types. When a variable is defined in C++, the compiler automatically allocates some memory for that variable depending upon the data type with which it has been declared.

What is a data type in C++?

In C++, a data type specifies the type of data that a variable can hold, such as integers, floating-point numbers, characters, and more. It determines the size and layout of the variable's memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Types of Data Types in C++

There are broadly three types of data types in C++: Primitive data type, Derived data type, User Defined data type. The programmer can choose the data type best suited to the requirements of the application because C++ provides a wide range of data types. The size and type of values that will be saved are specified by data types. Although C++ instructions are the same across all platforms, the storage representation and machine instructions used to modify each data type vary from one machine to the next.

Primitive Data Types in C++

Primitive data types are the data types that are built-in or are predefined data types and are directly available for the user to use. Various types of Primitive data types are

1. Integer 

  • The Keyword used for Integer is int.
     
  • The Size of the Integer is 4 bytes.
     
  • Description: Stores whole numbers without decimals
     
  • The Range is from -2147483648 to 2147483647.

2. Character 

  • The Keyword used for Character is char.
     
  • The Size of the Character is 1 byte.
     
  • Description: Stores characters/letters/number/ASCII values.
     
  • The Range is from -128 to 127 or 0 to 255.

3. Boolean 

  • The Keyword used for Boolean is bool.
     
  • The Size of Boolean is 1 byte.
     
  • Description: Stores boolean or logical values,i.e., true or false.
     
  • The Range is either true or false.

4. Floating Point  

  • The Keyword used for Floating Point is float.
     
  • The Size of Floating Point is 4 bytes.
     
  • Description: Stores single-precision decimal values or floating-point values
     
  • Precision: 7 decimal digits of precision

5. Double Floating Point 

  • The Keyword used for Double Floating Point is double.
     
  • The Size of Double Floating Point is 8 bytes.
     
  • Description: Stores decimal values or double-precision floating-point values
     
  • Precision:15 decimal digits of precision

6. Void or Valueless

  • Description: Used for those functions which don’t return a value
     
  • Good for processes that carry out activities but do not result in a useful output
     
  • Particularly in bigger software projects, they are important in structuring and organizing code

7. Wide Character

  • The Keyword used for Wide Character is wchar_t.
     
  • The Size of Wide Character is 2 or 4 bytes.
     
  • Description: Similar to character data type, but it has a size that is greater than the standard 8-bit datatype
     
  • The Range is 1 wide-character.
     

You can also do a free certification of Basics of C++.

Derived Data Types in C++

These are those data types that are derived from the built-in or so-called primitive data types. Various types of Derived data types are:

1. Function 

 It is defined as a piece or block of code that is defined to perform a specific, well-defined task. Functions are usually defined to save the user from writing the same lines of codes again and again for the same input. Functions make the code organised and reduce redundant lines of code. A function can be called anywhere required that too any number of times.

 

Syntax: Function_Type Function_Name( Parameters )

2. Array 

It is defined as the collection of similar items stored at continuous memory locations. The idea of using an array is to represent many instances in one variable.

 

Syntax: DataType Array_Name( size_of_array );

3. Pointers

 Pointers refer to a variable that holds the address of another variable; like any other variable, they too have a data type. It allows programs to simulate call-by-reference as well as create and manipulate dynamic data structure.

 

Syntax: DataType *variable_name;

4. References 

A reference variable is an alias, i.e., another name given to a variable that already exists. A reference variable gets attached to the memory location of the original variable, thereby allowing the user to access the contents of the variable by either the original variable name or the reference.

 

Syntax: DataType& reference_variable_name = original_variable ;

Abstract or User-Defined Data Types in C++

The data types that are defined by the user themselves are known derived data types or user-defined data types. Various types of User Defined data types are:

1. Class

It consists of data members and member functions, which can be accessed and used by creating an instance (object) of the class. A class is a kind of blueprint for an object. Classes are the building block for C++ that leads to Object-Oriented Programming. Classes are created using the “class” keyword.

Classes are the building blocks of object-oriented programming and are used to create objects by encapsulating data and behaviour. Developers provide a systematic blueprint for organizing and managing data in a more streamlined and modular manner by defining classes with the "class" keyword.

Syntax: 

Class ClassName
{
	Access Specifer // Can take any one of the three values
	// Private, Protected, Protected
	Data members // variables to be used
	Member functions // Methods/Functions to access the data members
}; // a class always ends with a semicolon

2. Structure

A structure creates a data type that is a collection of variables of different data types grouped together under a single name/type. A structure is declared using the struct keyword.

 

Syntax: struct Structure_Name{ // variables of different data types };

 

This syntax defines a C++ structure named "Structure_Name" that can hold variables of various data types. Structures are used to group related data together in a single unit.

3. Union

In union, all the members which are defined share the same memory location, i.e., any change brought in one member of the union will be reflected in all other members.

 

Syntax: union Union_name{ // data members };

4. Enumeration 

It is a user-defined data type whose main purpose is to make the program easy to read and maintain. It is basically used to assign meaning full names to integral constants. To define an enumeration, the keyword enum is used.

 

Syntax: enum flag{constant1, constant2, constant3, ....... };

Datatype Modifiers in C++

Datatype modifiers are used together with built-in data types to adjust the amount of data that a given data type may contain.

Data Type Modifiers are keywords in C++ that are used to change the properties of a data type's current properties. The types of datatype modifiers are as follows:

  • long
  • short
  • unsigned
  • signed

Let us look at each one's size and range with the help of a table.

Data Type Size (in bytes) Range
short int 2 -32,768 to 32,767
 
unsigned short int 2 0 to 65,535
unsigned int 4 0 to 4,294,967 ,295
int  4 -2,147,483,648 to
2,147,483,647
long int 4 -2,147,483,648 to
2,147,483,647
unsigned long int 8 0 to 4,294,967,295
long long int  8 -(2^63) to (2^63)-1
unsigned long long int 8 O to
18,446,744,073,709,551,615
signed char 1 -128 to 127
unsigned char  1 0 to 255
float 4 -
double 8 -
long double 12 -
wchar_t 2 or 4 1 wide character

Frequently Asked Questions

What are the derived data types in C++?

Derived data types in C++ are built from primitive or fundamental data types and include arrays, functions, pointers, and references. They allow for more complex data structures and operations, expanding the language's capability to handle diverse programming needs.

What are the 3 data types in C++?

The 3 types of data types in C++ are Built-in, user-defined, and derived. Built-in data types in C++ are pre-defined data types, user-defined data types are defined by the user, and derived are derived from existing data types in C++.

Is string a data type in C++? 

No, Strings are used to store a series of characters or text. This is not a built-in type but functions similarly in its most basic application. String values must be enclosed in double quotation marks.

Conclusion

The most important concept to understand before beginning the coding portion of the language is the Data type. The type of data that every variable could hold is known as the data type, and examples include character, float, and integer data types. Every language has a variety of data types, thus studying each type in depth can help us use them effectively and accurately.

To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course. We hope this article helped you in understanding the various C++ Data Types. You can read more such articles on our platform, Coding Ninjas Studio.

 

Previous article
Reverse a String in C++
Next article
Smart Pointers in C++
Live masterclass