Data Types in C++

Data Types in C++

In this tutorial you will learn about different types of data types in C++. You will read about enumerated data types, primitive data types and modifiers in C++.

While writing computer program in a computer language, we usually need to use variables to store the data taken from user or to store the produced information. This data or information may be of different kinds. Before storing this data/information in variables we need to declare variables and at the time of a variable declaration we will be required to mention that what kind of data/information will be stored in a variable.

For this purpose different data types are available in C++ which can be used to declare variables and tell the computer that what kind of data will be stored in those variables. Once data type is assigned to a variable then the same type of data must be stored in that variable otherwise we will get errors either at the time of compilation or at run time.

 

Primitive Data Types in C++

The primitive data types available in C++ are as below. All these are predefined and can be used directly without any additional requirements.

Data Type

Description

Size in Bytes

Value Range

char Used to store a single character. The character may be alphabetic, numeric or any special character

1

-127 to 127 or 0 to 255

int Used to store integer values

4

-2147483648 to 2147483647

float Used to store floating point values

4

+/- 3.4e +/- 38 (~7 digits)

double Double precision floating point values. This data type is considered more accurate and fast than double data type

8

+/- 1.7e +/- 308 (~15 digits)

bool Boolean values

1

true or false

wchar_t Wide character. It is used for storing compiler-defined wide characters, which may be unicode characters

2 or 4 bytes

1 wide character

void Used with functions or pointer variables

NA

NA

 

Modifiers in C++

The value range of some of the above primitive data types can be changed with the signed, unsigned, long and short modifiers as mentioned in the table given below.

Data Type

Description

Size in Bytes

Value Range

signed char Signed character

1

-127 to 127 or 0 to 255

unsigned char Unsigned character

1

0 to 255

signed int Values may be either positive, negative or zero

4

-2147483648 to 2147483647

unsigned int Values may be either positive or zero. Negative values are not allowed

4

0 to 4294967295

short int Short integer values

2

-32768 to 32767

signed short int Same as short int

2

-32768 to 32767

unsigned short int Values may be either positive or zero. Negative values are not allowed

2

0 to 65,535

long int Long integer

4

-2,147,483,648 to 2,147,483,647

signed long int Same as long int

4

-2,147,483,648 to 2,147,483,647

unsigned long int Values may be either positive or zero. Negative values are not allowed

4

0 to 4,294,967,295

long double Long double precision

8

+/- 1.7e +/- 308 (~15 digits)

 

Enumerated Data Types

Enumerated data types allow us to define our own data types with some specific values. The syntax to define enumerated data type is as below.

enum enum-name { list of values } variables-list;

Here, enum is a keyword followed by the user-defined name. The list of variables are given within curly brackets separated by commas followed by the variables of this data type. An example of enumerated data type is given below.

enum names {Michael,Jhon,Williams,Strac,Jhonson} my_names;

The value of first name is set as 0, the value of second name is set as 1 and son. For example the following code will display 3 as the value of “Starc”.

Starc

We can change the default values of names as below. In this case the value of “Michael” is 0 while the value of “Jhon” is set as 10. The value of next name i.e. “Williams” will be set as 11 and so on. So if we display the value of “Starc” then we will get 13 as output.

Michael

More Related Articles For You

    C++ Tutorial

    C++ Quizzes

 

Search

Shortcodes Ultimate

 
Follow Us
 
BGP