Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Data Types

In this tutorial, we will learn about basic data types such as int, float, char, etc. in C++ programming with the help of examples.


Data Types

1) Data types define the type of data a variables can hold.
2) This determines the type and size of data associated with variables.
3) Every data type requires a different amount of memory.

For example,

int number = 20;

Here, age is a variable of type int. Meaning, the variable can only store integers of either 2 or 4 bytes.


C++ Types of Data Types

There are 3 types of data types in C++:
1) Primary (Primitive) Data Types
2) Derived Data Types
3) User Defined Data Types


C++ Primitive Data Types

These data types are build-in or predefined data types and can be used directly by the user to declare variables.

The table below shows the Primitive data types, their meaning, and their sizes (in bytes):

Data Type Meaning Size (in Bytes)
int Integer 2 or 4
float Floating-point 4
double Double Floating-point 8
char Character 1
wchar_t Wide Character 2
bool Boolean 1
void Empty 0

Now, let us discuss these Primitivedata types in more detail.


1. C++ int

1) The int keyword is used to indicate integers.
2) Integers typically requires 4 bytes of memory space.
3) It can store values from -2147483648 to 2147483647.

For example,

int age = 20;

2. C++ float and double

1) float and double are used to store floating-point numbers (decimals and exponentials).
2) float variable typically requires 4 bytes of memory space and double variable requires 8 bytes of memory space.
3) double has two times the precision of float.

For example:-

float area = 14.34;
double volume = 34.6534;

As mentioned above, these two data types are also used for exponentials.
For example,

double distance = 35E11    // 35E11 is equal to 35*10^11

3. C++ char

1) Keyword char is used for storing characters.
2) Character typically requires 1 byte of memory space.
3) Characters in C++ are enclosed inside single quotes ' '.

For example,

char car = 'c';

Note: In C++, an integer value is stored in a char variable rather than the character itself.


4. C++ wchar_t

1) Wide character wchar_t is similar to the char data type, except its size is 2 bytes instead of 1.
2) It is generally 2 or 4 bytes long.
3) It is used to represent characters that require more memory to represent them than a single char.

For example:-

wchar_t test = L'a'  // storing Hebrew character;

Notice the letter L before the quotation marks, to make wide-character we have to add ‘L’ before the character literal.

Note: There are also two other fixed-size character types char16_t and char32_t introduced in C++11.


5. C++ bool

1) Bool data type is used for storing boolean or logical values.
2) The bool data type can store either true or false.
3) Booleans are used in conditional statements and loops (which we will learn in later chapters).


6. C++ void

1) The void keyword indicates an absence of data. It means “nothing” or “no value”.
2) Void data type is used for those function which does not returns a value.
3) We will use void when we learn about functions and pointers.

Note: We cannot declare variables of the void type.


C++ Type Modifiers

We can further modify some of the fundamental data types by using type modifiers. There are 4 type modifiers in C++. They are:
1) signed
2) unsigned
3) short
4) long

We can modify the following data types with the above modifiers:
1) int
2) double
3) char


C++ Modified Data Types List

Data Type Size (in Bytes) Meaning
signed int 4 used for integers (equivalent to int )
unsigned int 4 can only store positive integers
short 2 used for small integers (range -32768 to 32767)
long at least 4 used for large integers (equivalent to long int)
unsigned long 4 used for large positive integers or 0 (equivalent to unsigned long int)
long long 8 used for very large integers (equivalent to long long int).
unsigned long long 8 used for very large positive integers or 0 (equivalent to unsigned long long int)
long double 8 used for large floating-point numbers
signed char 1 used for characters (guaranteed range -127 to 127)
unsigned char 1 used for characters (range 0 to 255)

Let’s see a few examples.

long a = 9325232;
long int d = 2315312;
long double b = 231414.26313;
short e = 1264233; // Error! out of range
unsigned int c = -9;    // Error! can only store positive numbers or 0

Derived Data Types

Data types that are derived from primitive or build-in data types are referred to as derived data types.
There are 4 types of derived data types :-
1) Function
2) Array
3) Pointer
4) Reference

We will learn about these derived data types in later tutorials.


User Defined Data Types

These data types are defined by user itself. User defined data type is also known as Abstract data types.
There are 4 types of User defined data types :-
1) Class
2) Structure
3) Union
4) Enumeration
5) Typedef defined Data Type

We will learn about these User-defined data types in later tutorials.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!