SEARCH THIS BLOG

DATATYPES IN C++

Unlike python, which understands the type of data on its own, C++ compiler needs complete information about the type of values that can be stored in a variable. This type is defined using datatypes. Once the type of variable is defined, compiler knows what space the variable would take and what operations are allowed on that variable.


DATATYPES IN C++ 


Datatypes in C++


 

 There are 3 types of data types in C/C++:

1.     Primitive/ Fundamental

2.    Derived

3.    User defined

Read more >>

Derived Datatypes in C++

User-defined Datatypes in C++

 

This article here discusses Primitive data types.

 

Primitive data types or Fundamental data types are those that are not composed of other data types. They are individual entities having predefined characteristics of their own.

 

6 mostly used primitive datatypes have been discussed below :

 

1.     Integer data type (int) : The keyword for integer data type is int. Integers are numbers without fractional parts. They occupy 4 bytes space in memory (according to latest Compilers). It is the most widely used data type in any computer program because of its moderate range and less space consumption.


2.    Character data type (char) : The keyword for character data type is char. A character type variable can store any value from the C++ character set. Character variables are basically integer types which trigger the compiler differently. Any character stored in the variable is stored as its ASCII value which is an integer. A character variable occupies 1 byte of space in memory.

      Read more >> Character set in C++


3.    Boolean data type (bool) : The keyword for Boolean data type is bool. A Boolean variable is used to store relational and logical values. It can store either true or false. It occupies 1 byte of space in memory.


4.    Floating point (float) : The keyword for floating point data type is float. They are used to store real (i.e. decimal point) numbers. They have a size of 4 bytes and are less precise than double.


5.    Double data type (double) : The keyword used for double data types is double. They also store real numbers but with greater precision (i.e. more significant figures) than float. They take 8 bytes of space in memory.


6.    Void data type (void) : The keyword used for void datatype is void. It is used to denote “nothing” or a valueless entity. As it doesn’t store any value, it takes no space in the memory.


 DATATYPE MODIFIERS 

Except type void, the basic datatypes may have various modifiers preceding them. As the name implies, a modifier can be used to alter the meaning of the base type to fit various situations more precisely.

The list of modifiers is : signed, unsigned, long, short

 

Datatypes in C++


As shown in the above diagram, we can apply signed, unsigned, long and double to int and signed, unsigned to char. However, we may also apply long to double. (Note that ANSI doesn’t allow for long float as it is nothing but double).

Let us discuss the modifiers in detail:

 

Integer types:

 

By using different number of bytes to store values, C++ offers three types of integers: short, int and long that can represent three different integer sizes. Each comes in both signed and unsigned format. That gives us a total of 6 of integer types. The sizes can vary from compiler to compiler but C++ offers a flexible standard with some guaranteed minimum sizes as:


A short integer is at least 2 bytes.


An int integer is at least as big as short.


A long integer is at least 4 bytes and at least as big as int.

 

Type

Approx size (in bytes)

Minimal Range

short

unsigned short

signed short

2

2

2

-32768 to 32767

0 to 65535

same as short

int

unsigned int

signed int

4

4

4

-(231) to +(231)-1

0 to (232)-1

same as int

long

unsigned long

signed long

4

4

4

same as int

same as unsigned int

same as int

 

Note : It is obvious from the table that short, int, long are signed by default. Also, the C++11 update also provides for a type long long int with size of at least 8 bytes.

 

Character Types:

 

The char type is really another integer type (as inside memory it actually holds numbers i.e. ASCII of the characters). It is guaranteed to be large enough to represent the entire character set of 256 characters. As we know 1 byte space holds a total of 256 numbers, therefore all char types have size of 1 byte.


Type

Approx Size (in byte)

Minimal Range

char

unsigned char

signed char

1

1

1

-128 to 127

0 to 255

same as char

 

Floating-point Types:

 

C++ has 3 floating types : float, double, long double. These are defined by the number of significant figures that they can hold (i.e. precision).


Types

Approx Size (in bytes)

Minimal Range

Digits of precision

float

double

long double

4

8

10

3.4 x 10-38  to 3.4 x 1038 -1

1.7 x 10-308 to 1.7 x 10308-1

3.4 x 10-4932 to 3.4 x 104932 -1

7

15

19

 

 The sizeof() Operator:

The size allotted to variables of different types are different. Also, the size of same type of variable can be different for different compilers. To cope with this difficulty, C++ provides an inbuilt function sizeof(data_type), which returns the size of the data type in bytes.


Output Screen :

Size of char is 1

Size of int is 4

Size of long int is 4

Size of long long int is 8

Size of float is 4

Size of double is 8

Size of long double is 16







To download this article in pdf form click here.

Don’t stop here. To continue reading more articles on C++ click here.

If you have any doubts or suggestions on new topics, kindly comment on this article.

If you want to contribute any article, share it on coding.nkcoder@gmail.com. We will publish it with your name.



Post a Comment