Video courses for company/skill based Preparation
Purchase mock tests for company/skill building
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-in, user-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
Description: It accepts numerical of decimal type either +ve or -ve
Range: -32768 to +32768
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
Description: It will accept only positive numbers
Range : 0 to 65535
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
Ticket numbers in railways are never negative
Description: It increases the range of integer datypes by 2 or 4 bytes
Range : -2147,483,648 to 2147,483,647
long int a=999999L; long int b=65536L;
Macros: LONG_MAX and LONG_MIN
Description: It accepts +ve numbers of the larger range
Range : 0 to 4,294,967,295
unsigned long int a=99999999LU; unsigned long int b=65533612LU;
Macros: ULONG_MAX and ULONG_MIN
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
1 byte (8 bits)
Range: -128 to 127
Macros: CHAR_MAX and CHAR_MIN
Description: It accepts more number of charecters than normal char type
2 bytes(16 bits)
Range : 0 to 255
unsigned char a='ß'//beta charecter has ASCII value 223
Description: It accepts fractional numbers up to 6 decimal places
Range : -3.4*10e8 to 3.4*10e8
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
8 bytes(64 bits)
Range : -1.7*10e308 to 1.7*10e308
float a=56.778786666666; float b=6.5555555555555;
Macros: DBL_MAX and DBL_MIN
long doubleDescription: 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
long double a=56.7787888888888888888;Macros: LDBL_MAX and LDBL_MIN
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.
wchar_t b='艾儿';//Chinese alphabet
- 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.
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.
- 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.
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.
bool x = 0; // false
bool y = 100; // true
bool z = 18.75; // true
C++ program demonstrating datatypes
int b=32769;//invaloid:loss of precison
long int c=32769;//valid because of enough size
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;
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
- Rage of datatypes always in between
-2n-1 to 2n-1-1and
0 to 2n -1for unsigned types, where n is the number of bits
- 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