Data types of Variables:

Datatypes specify the type of data strong in the variable. They restrict other types of data values 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++:

  • Primitive / Primary / Built-in data types
  • Derived data types
  • 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 stores decimal values but its precision is double than float.


datatype.cpp
#include <iostream>
using namespace std;
  
int main(){
    int intVal = 10;
    double doubleVal = 2.14;
    float floatVal = 32.0009;
    cout<<"Int : " << intVal <<endl;
    cout<<"Double : " << doubleVal <<endl;
    cout<<"Float : " << floatVal <<endl;
    return 0;
}

Scientific Numbers:

Scientific numbers are stored in double or float data type.

datatype.cpp
#include <iostream>
using namespace std;

int main(){
    float dia1 = 32e2;
    double dia2 = 32e4;
    cout<<"Value of dia1 = "<<dia1<<endl;
    cout<<"Value of dia2 = "<<dia2<<endl;
    return 0;
}

Boolean Data Type:

It stores only two values true or false. It returns either 1 when true or 0 when false.
Boolean stores:
  • true / false
  • Yes / No
  • 1 / 0
datatype.cpp
#include <iostream>
using namespace std;

int main(){
    bool isTrue = true;
    cout<<"isTrue = "<<isTrue<<endl;
    bool isFalse = false;
    cout<<"isFalse = "<<isFalse<<endl;
    return 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.

char grade = 'A'

char.cpp
#include <iostream>
using namespace std;

int main(){
    char grade = 'A';
    cout<<"My grade is "<<grade<<endl;
    return 0;
}

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

char.cpp
#include <iostream>
using namespace std;

int main(){
    char grade = 65;
    cout<<"My grade is "<<grade<<endl;
    return 0;
}

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 use to store sequence of characters/text/anything inside double quotation marks.

string.cpp
#include <iostream>
using namespace std;

int main(){
    string myName = "John Smith";
    cout<<"My name is "<<myName<<endl;
    return 0;
}

We will learn about string data type in detail in string 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 memorize the size of all data types.
sizeof() operator: If you want to find the size of a datatype, put it in sizeof() operator.


size.cpp
#include <iostream>
using namespace std;

int main(){
  cout<<"Size in bytes"<<endl;
  cout<<"Char: "<<sizeof(char)<<endl;
  cout<<"Bool: "<<sizeof(bool)<<endl;
  cout<<"Int: "<<sizeof(int)<<endl;
  cout<<"Float: "<<sizeof(float)<<endl;
  cout<<"Double: "<<sizeof(double)<<endl;
  return 0;
}

Similarly, we 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.

modifiers.cpp
#include <iostream>
using namespace std;

int main(){
    unsigned int a = 10;
    signed int b = -20;
    cout<<a<<endl;
    cout<<b<<endl;
    return 0;
}

ModifiersSize
int4 byte
signed int4 byte
unsigned int4 bytes
short2 bytes
unsigned short2 bytes
long2 bytes
signed long8 bytes
unsigned long8 bytes
signed short8 bytes

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