Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Predefined Datatypes in C++

Predefined datatypes in C++

Datatypes describe what type of data is stored in the variables
and describe the amount of memory required to be allocated for the data

Definition: It is a primary datatype that directly interacts with machine instructions

C++ offers the programmer a rich assortment of built-in as well as user-defined data types. 

Data types in C++ are categorized into three groups: Built-inuser-defined, and Derived

  • Built in: defined by designer in the library
    ex: int ,float etc
  • User defined: A new datatype created and customized by user from primitive types
    ex: enum, struct etc
  • Derived :They don’t create a new data type but use fundamental data type to add extra feature
    ex: Array,pointer etc
Data Types in C++

int

Description: It accepts numerical of decimal type either +ve or -ve
Size:2 bytes(16bits)
Range: -32768  to  +32768

Example:

int a=22;
int b=-32768;

Macros: For every dataype, we have macros to find out maximum and minimum values that the datatype can stores
Minimum value: INT_MIN
Maximum value: INT_MAX

unsigned int

Description: It will accept only positive numbers
Size: 2 bytes(16bits)
Range : 0 to 65535

Example

unsigned int a=32769;
unsigned int b=6520;

Macros: UINT_MAX and UINT_MIN
Some times we strictly require only positive numbers, in such case no need to generate the sign bit and it can also be used to store the data itself and can increase the range

Ex: Ticket numbers in railways are never negative

long int

Description: It increases the range of integer datypes by 2 or 4 bytes
Size: 4 bytes(32bits)
Range :  -2147,483,648  to  2147,483,647

Example

long int a=999999L;
long int b=65536L;

Macros: LONG_MAX and LONG_MIN

unsigned long

Description: It accepts +ve numbers of the larger range
Size: 4 bytes(32bits)
Range : 0  to  4,294,967,295

Example

unsigned long int a=99999999LU;
unsigned long int b=65533612LU;

Macros: ULONG_MAX and ULONG_MIN

char

Description: every key in keyboard acts 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

Example

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

Macros: CHAR_MAX and CHAR_MIN

unsigned char

Description: It accepts more number of charecters than normal char type
Size: 2 bytes(16 bits)
Range : 0 to 255

Example

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

float

Description: It accepts fractional numbers up to 6 decimal places
Size: 4 bytes(32bits)
Range :  -3.4*10e8  to  3.4*10e8

Example

float a=56.77878f;
float b=6.5f;

Macros: FLT_MAX and FLT_MIN

Description: It accepts fractional numbers up to 12 decimal places
mainly used for scientific computations
Size: 8 bytes(64 bits)
Range : -1.7*10e308 to 1.7*10e308

Example

float a=56.778786666666;
float b=6.5555555555555;

Macros: DBL_MAX and DBL_MIN

long double

Description: It accepts fractional numbers up to 19 decimal places mainly used for scientific computations Size: 10 bytes(80 bits) Range :  -3.4*10e4892 to 3.4*10e4892

Example

long double a=56.7787888888888888888;
Macros: LDBL_MAX and LDBL_MIN

wchar_t

Wide char can take on 65536 values which correspond to UNICODE values which is a recent international standard which allows for the encoding of characters for virtually all languages and commonly used symbols.

This data type occupies 2 or 4 bytes depending on the compiler being used.

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

Example

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

bool

  • 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 datatypes

main()
{
int a=567;//valid
int b=32769;//invaloid:loss of precison
long int c=32769;//valid because of enough size
char d='_'//underscorecharecter
double e=66.908909897;

cout<<"a:"<<a<<endl;
cout<<"b:"<<b<<endl;
cout<<"c:"<<c<<endl;
cout<<"d:"<<d<<endl;
cout<<"e:"<<e<<endl;

cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
cout << "\nshort int ranges from : " << SHRT_MIN << " to " << SHRT_MAX;
cout << "\nunsigned short int ranges from : " << 0 << " to " << USHRT_MAX;
cout << "\n int ranges from : " << INT_MIN << " to " << INT_MAX;

}

O/P

a:567
b=-32768
c:32769
d:_
e:66.908909897
Size of float : 4
Size of double : 8
Size of wchar_t : 4
short int ranges from : -32768 to 32767
unsigned short int ranges from : 0 to 65535
int ranges from : -2147483648 to 2147483647

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

One comment on “Predefined Datatypes in C++”