➤ Hello World! in C++
➤ Print Number in C++
➤ Add 2 Numbers C++
➤ Arithmetic Operation
➤ Sum Avg of 3 Number
➤ Area Program in C++
➤ Simple Interest in C++
➤ Find ASCII value in C++
➤ Swap 2 Number in C++
Flow Control Programs
➤ Even-Odd in C++
➤ +ve, -ve, 0 in C++
➤ Vowel-Consonant
➤ Greatest of 3 no.
➤ Check Leap Year
➤ Calculator Program
➤ Reverse a Number
➤ Sum of Natural Number
➤ GCD of 2 Number
➤ LCM of 2 Number
➤ Find Power in C++
➤ Fibonacci Series in C++
➤ Palindrome Number
➤ Find Factorial in C++
➤ Factorial Using Recursion
➤ Prime Number in C++
➤ Prime Number b/w 1-N
Array
➤ Linear Search in C++
➤ Binary Search in C++
Others
➤ Introduction to C++
➤ Data Types in C++
➤ Range of Data Types
➤ Void main, main vs int main
In this tutorial, we will know what is data types in C++ Programming Language. What is Data type modifiers? How to find the size of data types in C++ and the size of a variable in C++.
Data types define the type of data a variable can store. Whenever we declare a variable it reserves some memory locations to store various information. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
Mainly Data Types in C++ can be devided into two types:-
- Primitive data type:- These are predefined data types and can be used directly to declare a variable. The primitive data types are:- Integer, Character, Boolean, Floating point, Double Floating Point, Void or Valueless, and Wide Character.
Primitive Data Types | Keyword |
Valuless or void | void |
Boolean | bool |
Character | char |
Wide Character | wchar_t |
Integer | int |
Floating Point | float |
Double Floating Point | double |
2. User-defined data type:- These data types are defined by the user to declare the variable. Structure, union, enum are examples of user-defined data types.
Data Type Modifiers
These are used to modify the length of data that a particular data can hold. These modifiers are:- Signed, Unsigned, Short and Long
Examples are signed int, unsigned int, short int, long int, signed short int, unsigned short int. Note that these modifiers do not use with float and double (exception is long double).
If signed and unsigned not written with data types then by default it is signed data types. So, int and signed int are same.
Size of Data Types in C++
The size of data types is dependent on the compiler or you can say that the system architecture i.e. 32-bit compiler or 64-bit compiler. The size of data type int is 2 byte in 32-bit architecture or 4 bytes in 64-bit architecture. Another factor on which the size of data type depends is the compiler on which you perform any program.
For finding the size we need a sizeof() function.
C++ Program to Find the Size of Data Types
#include <iostream>
using namespace std;
int main()
{
cout << "Size of char = " << sizeof(char) << " byte" << endl;
cout << "Size of wchar_t = " << sizeof(wchar_t) << " bytes" << endl;
cout << "Size of short int = " << sizeof(short int) << " bytes" << endl;
cout << "Size of int = " << sizeof(int) << " bytes" << endl;
cout << "Size of unsigned int = " << sizeof(unsigned int) << " bytes" << endl;
cout << "Size of signed int = " << sizeof(signed int) << " bytes" << endl;
cout << "Size of long int = " << sizeof(long int) << " bytes" << endl;
cout << "Size of Float = " << sizeof(float) << " bytes" << endl;
cout << "Size of double = " << sizeof(double) << " bytes" << endl;
return 0;
}
Output:-
Size of char = 1 byte
Size of wchar_t = 4 bytes Size
of short int = 2 bytes
Size of int = 4 bytes
Size of unsigned int = 4 bytes Size
of signed int = 4 bytes
Size of long int = 8 bytes
Size of Float = 4 bytes Size
of double = 8 bytes
The size of signed and unsigned data types will be the same Because these modifiers change the range of data types not the size of data types. But, the Size of short and long data types may be different.
Size of a Variable in C++
#include <iostream>
using namespace std;
int main()
{
int x=10;
char ch='A';
cout << "Size of Variable x = " << sizeof(x) << " bytes" << endl;
cout << "Size of Variable ch = " << sizeof(ch) << " bytes" << endl;
return 0;
}
Output:-
Size of Variable x = 4 bytes
Size of Variable ch = 1 bytes
The Size of variable and data types are the same because we use variables to store the data.
The Range of Data Types in C++
Type | Typical Bit Width | Typical Range |
char | 1 byte | -127 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -127 to 127 |
short int | 2 bytes | -32768 to 32767 |
unsigned short int | 2 bytes | 0 to 65,535 |
signed short int | 2 bytes | -32768 to 32767 |
int | 4 bytes | -2147483648 to 2147483647 |
unsigned int | 4 bytes | 0 to 4294967295 |
signed int | 4 bytes | -2147483648 to 2147483647 |
long int | 4 bytes | -9223372036854775808 to 9223372036854775807 |
unsigned long int | 4 bytes | 0 to 18446744073709551615 |
signed long int | 4 bytes | -9223372036854775808 to 9223372036854775807 |
float | 4 bytes | +/- 3.4e +/- 38 (~7 digits) |
double | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
long double | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
wchar_t | 2 or 4 bytes | 1 wide character |
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!