Data Types and Modifiers in C++ - The Coding Shala

Home >> Learn C++ >> Data Types and Modifiers in C++

Data types and Modifiers in c++

In every program we need to store values for that variable are used. Variables are just reserved memory location to store values. every variable reserve some space in memory. Data Types are used to define the type of data a variable can hold, for example, a character type variable can hold character data, integer variable can hold integer data. data types are necessary to define with variables.

Data types in C++ are divided into three groups -
  1. Primitive Data types or Built-in Data Types
  2. Abstract or user-defined data types
  3. Derived Datatypes
Data Types in C++ - The Coding Shala
In this post we are going to discuss primitive data types only, user-defined and derived data types will cover in later posts.

Primitive or Built-in Datatypes

These data types are predefined data types in c++ and we can use these data types directly to declare variables. The following image shows data types and their Keywords in C++ -
Data Types and Keywords in C++ - The Coding Shala
The following c++ program shows the basic use of Data types in c++ - 


#include <iostream>
using namespace std;

int main() {
 int count = 1;  //this is integer
 char a  = 'a';  //this is char
 int a1 = 5;
 int b1 = 2;
 cout<<a1<<" "<<b1<<endl;;
 int ans1 = a1/b1;
 float ans2 = a1/b1;
 float a2 = 5;
 float b2 = 2;
 float ans3 = a2/b2;
 cout<<a<<endl;
 cout<<ans1<<endl; //integer
 cout<<ans2<<endl;
 cout<<ans3<<endl; //floating
 return 0;
}



Output:: 
5 2
a
2
2
2.5

Datatype Modifiers in c++

Modifiers in c++ are used with the built-in data types to modify the length of data that a data type can hold. Data types Modifiers available in c++ are -
  • Signed
  • Unsigned
  • Short
  • Long
The following image shows the modified size of data types when we use modifier -
Data Types and Their Size in C++ - The Coding Shala
The size of these data types can vary depending on the compiler and computer architecture. We can display the size of data types using the size_of() function. The following c++ program displays the size of data types -

#include<iostream>
using namespace std;
 
int main()
{
    cout << "Size of char : " << sizeof(char) << " byte" << endl;
    cout << "Size of int : " << sizeof(int) << " bytes" << endl;
    cout << "Size of short int : " << sizeof(short int) << " bytes" << endl;
    cout << "Size of long int : " << sizeof(long int) << " bytes" << endl;
    cout << "Size of signed long int : " << sizeof(signed long int) << " bytes" << endl;
    cout << "Size of unsigned long int : " << sizeof(unsigned long int) << " bytes" << endl;
    cout << "Size of float : " << sizeof(float) << " bytes" <<endl;
    cout << "Size of double : " << sizeof(double) << " bytes" << endl;
    cout << "Size of long double :" << sizeof(long double) <<" bytes" <<endl;
    cout << "Size of wchar_t : " << sizeof(wchar_t) << " bytes" <<endl;
    return 0;
}



Output::
Size of char : 1 byte
Size of int : 4 bytes
Size of short int : 2 bytes
Size of long int : 8 bytes
Size of signed long int : 8 bytes
Size of unsigned long int : 8 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of long double :16 bytes
Size of wchar_t : 4 bytes



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Add two numbers in Scala - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

New Year Chaos Solution - The Coding Shala

Goal Parser Interpretation LeetCode Solution - The Coding Shala