The C and C++Programming Language

champak
₹2,000.00 ₹300.00
  • 0 student
  • 2 lessons
  • 0 quizzes
  • 10 week duration

C++ Data Types

Data Types in C++

These are the  7  C++ Data Types −

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

Data Types can be modified using the following keywords −

  • signed
  • unsigned
  • short
  • long

The following table shows the variable type, how much memory it takes to store the value in memory, and what is maximum and minimum value which can be stored in such type of variables.

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

Size of variables can change on the basis of compilers.

Program to display size of data types


#include <iostream>

using namespace std;

int main() {
cout << "Size of char : " << sizeof(char) <<"\n";
cout << "Size of int : " << sizeof(int) << "\n";
cout << "Size of short int : " << sizeof(short int) <<"\n";
cout << "Size of long int : " << sizeof(long int) << "\n";
cout << "Size of float : " << sizeof(float) << "\n";
cout << "Size of double : " << sizeof(double) << "\n";
cout << "Size of wchar_t : " << sizeof(wchar_t) << "\n";

return 0;
}

Output

 

The above program can be also made using variables.


#include &lt;iostream&gt;

using namespace std;

int main() {
char ch;
int i;
short int s;
long int l;
float f;
double d;
wchar_t w;
cout << "Size of char : " << sizeof(ch) <<"\n";
cout << "Size of int : " << sizeof(i) << "\n";
cout << "Size of short int : " << sizeof(s) <<"\n";
cout << "Size of long int : " << sizeof(l) << "\n";
cout << "Size of float : " << sizeof(f) << "\n";
cout << "Size of double : " << sizeof(d) << "\n";
cout << "Size of wchar_t : " << sizeof(w) << "\n";

return 0;
}

 

Read the program below and try to interpret the output.


#include &lt;iostream&gt;

using namespace std;

int main() {

short int si=32767;
unsigned short int ui=32767;

cout << "Value of signed int : " << si << "\n";
cout << "Value of unsigned int : " << ui <<"\n";
si++;
ui++;
cout << "Value of signed int : " << si << "\n";
cout << "Value of unsigned int : " << ui <<"\n";
si=10;
ui=10;
cout << "Value of signed int : " << si << "\n";
cout << "Value of unsigned int : " << ui <<"\n";
si=si+ 65536;
ui=ui+ 65536;
cout << "Value of signed int : " << si << "\n";
cout << "Value of unsigned int : " << ui <<"\n";
return 0;
}

&amp;nbsp;

 

 

Macros that give the ranges of Data Types are stored in limits.h


// C++ code to demonstrate the macros for data types
#include<iostream>
#include<limits.h> // for int,char macros
#include<float.h> // for float,double macros
using namespace std;
int main()
{

// Displaying ranges with the help of macros
cout << "char ranges from : " << CHAR_MIN << " to " << CHAR_MAX;
cout << "\n\nshort char ranges from : " << SCHAR_MIN << " to " << SCHAR_MAX;
cout << "\n\nunsigned char ranges from : " << 0 << " to " << UCHAR_MAX;

cout << "\n\n\nshort int ranges from : " << SHRT_MIN << " to " << SHRT_MAX;
cout << "\n\nunsigned short int ranges from : " << 0 << " to " << USHRT_MAX;
cout << "\n\nint ranges from : " << INT_MIN << " to " << INT_MAX;
cout << "\n\nunsigned int ranges from : " << 0 << " to " << UINT_MAX;
cout << "\n\nlong int ranges from : " << LONG_MIN << " to " << LONG_MAX;
cout << "\n\nunsigned long int ranges from : " << 0 << " to " << ULONG_MAX;
cout << "\n\nlong long int ranges from : " << LLONG_MIN << " to " << LLONG_MAX;
cout << "\n\nunsigned long long int ranges from : " << 0 << " to " << ULLONG_MAX;

cout << "\n\n\nfloat ranges from : " << FLT_MIN << " to " << FLT_MAX;
cout << "\n\nnegative float ranges from : " << -FLT_MIN << " to " << -FLT_MAX;
cout << "\n\ndouble ranges from : " << DBL_MIN << " to " << DBL_MAX;
cout << "\n\nnegative double ranges from : " << -DBL_MIN << " to " << +DBL_MAX;

return 0;

}

Let us try some programs with float and double.


#include <iostream>
#include<limits.h> // for int,char macros
#include<float.h> // for float,double macros
using namespace std;

int main() {

float f=FLT_MAX;
double d=DBL_MAX;

cout << "Value of float : " << f << "\n";
cout << "Value of double : " << d <<"\n";

f=f+f;
d=d+d;
cout << "Value of float : " << f << "\n";
cout << "Value of double : " << d <<"\n";

return 0;
//return 0;
}

end

Leave a Reply

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