Predefined Datatypes in C++

Predefined Datatypes

Here,  we will discuss predefined data types in C++. There are various predefined datatypes in C++ and a program should at least be aware of them and what their uses are.

Predefined Datatypes in C

1. Integer DataType

We will discuss the following –

  1. Default int
  2. Unsigned int
  3. Long int
  4. Unsigned long int
  5. Short int
  6. Unsigned short int

1. Default int

  • Description: It can accept a non-decimal value in both negative and positive ranges. Thus, the name integers.
  • Size: 2 or 4 bytes
  • Range: -32768  to +32768 or -2,147,483,648 to +2,147,483,648
  • Macros : (INT_MIN, INT_MAX)

Example:

int a = 10;
int b = -3512;

2. Unsigned int

  • Description: It can accept only positive values. A good use case for applications where only positive numbers are required example – Roll No., ticket system.
  • Size: 2 or 4 bytes
  • Range: to +65,535 or 0 to +4,294,967,295
  • Macros : (USHRT_MAX)

Example:

unsigned int a = 10;
unsigned int b = 10987;

3. long int

  • Description: Ideal for very large integer values in positive/negative ranges.
  • Size: 4 or 8 bytes
  • Range: -2,147,483,648  to +2,147,483,647 or -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
  • Macros : (LONG_MIN, LONG_MAX)

Example:

long int a = 10172817;
long int b = -9876543210123;

4. Unsigned long

  • Description: Ideal for very large integer values only in positive ranges.
  • Size: 4 or 8 bytes
  • Range: (0 to 4,294,967,295) or (0 to 18,446,744,073,709,551,615)
  • Macros : (ULONG_MAX)

Example:

long int a = 3120172817;
long int b = -9876512323443210123;

5. Short

  • Description: Ideal for very small integer values in positive/negative ranges.
  • Size: 2 bytes guaranteed
  • Range: (-32,768 to 32,767)
  • Macros : (SHRT_MIN, SHRT_MAX)

Example:

long int a = 12;
long int b = -1087;

6. Unsigned short

  • Description: Ideal for very small integer values only in positive ranges.
  • Size: 2 bytes guaranteed
  • Range: (0 to 65,535)
  • Macros : (USHRT_MAX)

Example:

long int a = 1027;
long int b = 10;

2. Char DataType

We will discuss the following in this –

  1. Default char
  2. Unsigned char

1. Default char

  • Description: Every key in the keyboard acts as a char with an ASCII value. Characters are declared in the single quotation to distinguish them from variable names and character values
  • Size: 1 byte (8 bits)
  • Range: -128 to 127
  • Macros: CHAR_MAX and CHAR_MIN

Example

char a='@';
char b='c';

2. Unsigned char

  • Description: It accepts more number of characters than normal char type
  • Size: 1 bytes(8 bits)
  • Range : 0 to 255
  • Macro: UCHAR_MAX

Example

unsigned char a='ß'//beta charecter has ASCII value 223

3. float DataType

  • Description: It accepts fractional numbers up to 6 decimal place precision
  • Size: 4 bytes(32bits)
  • Macros : FLT_MAX and FLT_MIN

Range :

  • In positive range : +1.17549e-38 to +3.40282e+38
  • In negative range : -1.17549e-38 to -3.40282e+38

Note: 1.17549e-38 means 1.17549*10^(-38) which is very very small close to zero

Example

    float a = 56.771; 
    float b = 6.5;
    float c = 1.23e10; // 1.23 * 10^10
    float d = 1.98e-12;// 1.98 * 10^(-12)

4. Double DataType

  • Description: It accepts fractional numbers up to 12 decimal places mainly used for scientific computations
  • Size: 8 bytes(64 bits)
  • Macros: DBL_MAX and DBL_MIN

Range :

  • In positive range : +2.22507e-308 to +1.79769e+308
  • In negative range : -2.22507e-308 to -1.79769e+308

Note: 2.22507e-308 means 2.22507*10^(-308) which is very very small (close to zero).

Example

    // same numbers as float
    // but below have higher precision and higher range
    double a = 56.771; 
    double b = 6.5;
    double c = 1.23e10; // 1.23 * 10^10
    double d = 1.98e-12;// 1.98 * 10^(-12)

5. Long Double DataType

  • Description: It accepts fractional numbers up to 19 decimal places mainly used for scientific computations
  • Size: 16 bytes(128 bits)
  • Macros: LDBL_MAX and LDBL_MIN

Range :

  • In positive range : +3.3621e-4932 to +1.18973e+4932
  • In negative range : -3.3621e-4932 to -1.18973e+4932

Note: 3.3621e-4932 means 3.3621e *10^(4932) which is very very small (close to zero).

Example

    // same numbers as float/double
    // but below have higher precision and higher range
    long double a = 56.771; 
    long double b = 6.5;
    long double c = 1.23e10; // 1.23 * 10^10
    long double d = 1.98e-12;// 1.98 * 10^(-12)

6. wchar_t DataType

  • Description: Wide char can take on 65536 values which correspond to UNICODE values which is a recent international standard that allows for the encoding of characters for virtually all languages and commonly used symbols.
  • Size: 16 bytes(128 bits)
  • Range :0 to 65,535 or 4294967296

Mostly the wchar_t data type is used when international languages like Chinese, French are used.

Example

wchar_t b = L'艾儿';//Chinese alphabet

7. bool DataType

  • The ISO/ANSI C++ Standard has added certain new data types to the original C++ specifications.
  • They are provided to provide better control in certain situations as well as for providing conveniences to C++ programmers.

Syntax:

bool b = true; // declaring a boolean variable with true value
  • In C++, the data type bool has been introduced to hold a boolean value, true or false.
  • The values true or false have been added as keywords in the C++ language.

Note:

  • The default numeric value of true is 1 and false is 0.
  • We can use bool type variables or values true and false in mathematical expressions also.

Example

int x = false + true + 5;//6
  • The above expression is valid and the expression on the right will evaluate to 6 as false has value 0  and true will have value 1.
  • It is also possible to convert implicitly the data type integers or floating-point values to bool type.

Example:-

bool x = 0; // false 
bool y = 100; // true 
bool z = 18.75; // true

C++ program demonstrating predefined datatypes

Run
#include<iostream>
using namespace std;

int main()
{
    int a = 567; //valid  
    int b = 2147483800; //invalid : will result in loss of precison max int value 2147483647
    long long int c = 2147483800; // valid because of enough size max long int value 9,223,372,036,854,775,807
    char d ='_'; //underscorecharecter
    double e = 66.91;
    bool f= false;
    bool g= true;
  
    cout << "a:" << a << endl;
    cout << "b:" << b << endl; // incorrect value will be printed due to overflow
    cout << "c:" << c << endl;
    cout << "d:" << d << endl; 
    cout << "e:" << e << endl;
    cout << "f:" << f << endl;
    cout << "g:" << g << endl;
    
    return 0;
}

Output

a:567
b:-2147483496
c:2147483800
d:_
e:66.91
f:0
g:1

Important Points

  1. Rage of datatypes always in between  -2n-1 to 2n-1-1   and 0 to 2n -1 for unsigned types, where n is the number of bits

Example:

  • int: -215 to 215-1 i.e -128 to 127
  • unsigned int: 0 to 216-1 i.e 0 to 65535

2. -ve numbers are always stored in,2’s compliment form hence you can allocate with +1 in the maximum negative range

Example:

-32768,-128 etc

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

One comment on “Predefined Datatypes in C++”