×

Search anything:

Fundamental Data Types in C++

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 30 minutes | Coding time: 5 minutes

In C++, every variable use data types to define the type of data it can store.
As, we already know variables are something which stores data.So,data types are used to specify before the compilation that what type of data the variable is going to store.

C++ is said to be stongly typed language which actually means we have to define the data type of the variable before using it.Each data type has its own size in memory.

There are mainly two types of data types in C++:-

  • Fundamental data types
  • User defined data types

Note:-This article will give you a deep understanding about the fundamental data types.

Fundamental Data types

Fundamental data types are the data types which are predefined in the language and can be directly used to declare a variable in C++.

The various fundamental data types are:

  1. char (Character)
  2. int (Integer)
  3. float (Floating point)
  4. bool (Boolean)
  5. double (Double floating point)
  6. void (valueless)

Now, let's talk about each one of them.

1. Character

  • It is the datatype that is used to store characters like a,b,c etc.
  • To define Character in C++ we use char keyword.
  • Size of char datatype is 1 byte i.e 8 bits.
  • It's range is from -128 to 127 or it can be 0-255.

Syntax:-

char a;

where 'a' is variable.

2. Integer

  • It is the datatype that is used to store integer values like 1,2,-1 etc.
  • To define Integer in C++ we use int keyword.
  • Size of int datatype is 4 byte i.e 32 bits.
  • It's range is from -2147483648 to 2147483647.

Syntax:-

int a;

3. Floating point

  • It is the datatype that is used to store floating values or decimal values like 1.1,2.1 etc.
  • To define Floating point in C++ we use float keyword.
  • Size of float datatype is 4 byte i.e 32 bits.
  • It's range is from +/- 3.4e +/- 38 (~7 digits).

Syntax:-

float a;

4. Boolean

  • It is the datatype that is used to store two values only i.e true or false.
  • To define Boolean in C++ we use bool keyword.
  • Size of bool datatype is 1 byte i.e 8 bits.

Syntax:-

bool a;

5. Double floating point

  • It is the datatype that is used to store floating values with higher precision.
  • To define Double Floating Point in C++ we use float keyword.
  • Size of double datatype is 4 byte i.e 32 bits.
  • It's range is from +/- 1.7e +/- 308 (~15 digits).

Syntax:-

double a;

6. Valueless

  • It is the datatype that is used to represent a variable without any value.
  • To define Valueless in C++ we use void keyword.
  • Void can be used as an return type of functions which indicates that function is returning nothing.

Syntax:-

void a;

A simple use of void as a return type of a function in C++:

    void printmsg(void)
{
     printf("Hello world\n");
}
  • Here the return type of function printmsg is void which means it is returning nothing.
  • void inside the parenthesis shows that the function has no or valueless arguement.

Data type modifiers

  • As the name suggests ,these are used to modify primitive data type i.e int,char,float.
  • They are used as a prefix to the primitive data types.
  • These modifiers change the size and the type of values that a primitive data type can hold.

There are four modifiers:

  1. signed:- It is used to represent numbers both in the positive and negative ranges.
    Syntax:
signed int a;

"a" can hold both negative and positive values.

  1. unsigned:- It is used to represent numbers in the positive only.

Syntax:

unsigned int b;

"b" can hold only positive values no negative values.

  1. short:- It is used to decrease the size of primitive data types.

Syntax:

short int c;

"c" will now take 2 bytes of memory instead of 4 bytes.

  1. long:- It is used to increase the size of primitive data types.

Syntax:

long int d;

The table below shows the size and range of data types :

Data Type Size Range
short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65,535
unsigned int 4 0 to 4,294,967,295
int 4 -2,147,483,648 to 2,147,483,647
long int 4 -2,147,483,648 to 2,147,483,647
unsigned long int 4 0 to 4,294,967,295
long long int 8 -(2^63) to (2^63)-1
unsigned long long int 8 0 to 18,446,744,073,709,551,615
signed char 1 -128 to 127
unsigned char 1 0 to 255
float 4 +/- 3.4e +/- 38 (~7 digits)
double 8 +/- 1.7e +/- 308 (~15 digits)

Note: Notice the range in case of unsigned and signed.Also these range can vary according to the compiler.

User defined data types in C++ are categorized into following:

  1. Classes
  2. Union
  3. Structure
  4. Enumeration

As this article mainly focuses on fundamental data types, we will discuss about user defined data types in separate articles.

Fundamental Data Types in C++
Share this