Data types of Variables

Datatypes specify the type of data strong in the variable. They restrict other types of data to be stored in the variable. For example, if we have int type var, it will not store float or char value.

Basically, there are three types of data types in C++:

  1. Primitive / Primary / Built-in data types
  2. Derived data types
  3. User-defined data types

Primitive Data Types

Primitive data types are the fundamental data types that are used to store basic values like numbers, characters, and boolean values. These data types represent single values and are not composed of other data types.

For Numbers

  • int - stores integer value.
  • float - it stores decimal values.
  • double - it also stores decimal values but its precision is double than float.

Scientific Numbers

Scientific numbers are stored in double or float data type.

Boolean Data Type

It stores only two values true or false. It returns 1 when true and 0 when false.

Boolean stores:

  1. true / false
  2. Yes / No
  3. 1 / 0

Practical Use

You can use it to set value of a variable using conditional statement which we will learn in future. For example, if Boolean type var istrue is true then set the value of int type var 10 otherwise 15.

Char Data Type

It stores single character in single quotes.

You can also assign ASCII values of characters to a char type variable.

Point: As beginners, some people will think that it should print 65. See carefully it is a character type variable, not integer type. If we assign a number to char type variable, then it will print character at that ASCII value.

String Data Type

String data type is used to store sequence of characters/text/anything inside double quotation marks.

We will learn about string data type in detail in later tutorial.

Size of variables

Size of variable depends upon the data type of variable.

Data TypeSize
char1 byte
bool1 byte
int2 or 4 bytes
float4 bytes
double8 bytes

Note: It is not important to remember the size of all data types.

sizeof() operator

If you want to find the size of a datatype, put it in sizeof() operator.

Similarly, you can also find the size of a variable.

Modifiers

Modifiers are used to define data types more precisely. They are used with primitive data types.

ModifiersSize
int4 bytes
signed int4 bytes
unsigned int4 bytes
short2 bytes
signed short2 bytes
unsigned short2 bytes
long8 bytes
signed long8 bytes
unsigned long8 bytes

Note: Do not need to worry about their details now. You will manage them automatically by doing practice in future.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

Which data type is used to store decimal numbers?

10 XP~2 min
Exercise 2 of 2Medium

What is the typical size of an int data type?

15 XP~3 min