ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

04 – C++ : Data Types

Cplusplus

Variables play an important role in programming because they simply serve as containers which hold values. Data types are use when declaring a variable to specify the action that you want to perform, it may be a string, integer, character, boolean, double, floating point and etc.

 

See below the seven basic C++ data types:

Type Keyword
Boolean bool
Character char
Double floating point double
Integer int
Floating point float
Valueless void
Wide character wchar_t

 

The following are type modifiers that can be used to modify several of the basic data types that was shown above:

  • short
  • long
  • signed
  • unsigned

 

In the table below are the list of variable types. You will see how much memory would it takes to store the value in memory and also the minimum and maximum value to be stored.

Type Typical Bit Width Typical Range
char 1byte -128 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -128 to 127
int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed short int 2bytes -32768 to 32767
long int 4bytes -2,147,483,648 to 2,147,483,647
signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int 2bytes 0 to 65,535
signed long int 4bytes -2,147,483,648 to 2,147,483,647
unsigned long int 4bytes 0 to 4,294,967,295
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15 digits)
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 bytes 1 wide character

 

 

This a procedure to generates the correct size of the data types on the computer. See it below.

#include <iostream>
using namespace std;

int main()
{

 cout << "Size of long int : " << sizeof(long int) << endl;
 cout << "Size of float : " << sizeof(float) << endl;
 cout << "Size of double : " << sizeof(double) << endl;
 cout << "Size of char : " << sizeof(char) << endl;
 cout << "Size of int : " << sizeof(int) << endl;
 cout << "Size of short int : " << sizeof(short int) << endl;
 cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
 return 0;
}

 

This is the result when the code is perform.

resultsCMD

 

 

Facebook Comments

Check Your Domain Ranking

Leave a Reply

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

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!