Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - DATA TYPES


We have seen that a variable is used to hold a value. Be it a number or a name or even a sentence.


i.e.


int x = 5;

Or


string y = "Hello World";

But how will C++ come to know, if the value is a number or a name?


And thus Data Types comes into picture.


Data Types


Data Types are the hidden gem that is used by C++ to determine if the variable is going to hold a number or a name.


But why does C++ need to determine if the variable it is going to hold be a number or a name? Can it not create one type of variable and store all the values there.


It cannot do that. Because if you want to store a number. It would take less space.

java_Collections

On the other hand, if you want to store a String like Hello World. It would take a larger space.

java_Collections

So, whenever you want to create a string variable and assign a string to a variable.


string y = "Hello World";

And C++ finds out that it is a String type value and creates the storage space accordingly.

java_Collections

The above Data Type for String is called string Data Type.


And similarly for a number, for a decimal value e.t.c. C++ has different Data Types.


C++ Data Types are categorised as :

  1. String
  2. Character
  3. Number
  4. Boolean
  5. Array

Data Types used in C++

  1. String



    As we have seen in the above example, a string is a set of letters/alphabets.

    The Data Type for String is string.

    string y = "Hello World";


    The value assigned to a string type variable must be in Double Quotes (i.e. "").

    Example :



    #include <iostream>
    using namespace std;
    
    int main() {
    
    	string x = "Hello World";
    	cout << x;
    	
    	return 0;
    }
    


    Output :



      Hello World


    In the above code we have initialised the variable x with the String Hello World.

    string x = "Hello World";


    Now, the string data type before the variable x will make sure that no values other than String would be allowed in it.

  2. Character



    In the above example i.e. String, we have seen how can a String variable hold a set of letters/alphabets.

    Now, what if we need a variable that would just hold one letter/alphabet.

    And yes, C++ provides that as well. i.e. A Char Data Type would hold a letter/alphabet.

    The only thing you need to keep in mind is, place the letter/alphabet inside single quotes ().

    Let us see in the below example,

    Example :



    #include <iostream>
    using namespace std;
    
    int main() {
    
    	char x = 'Y';
    	cout << x;
    	
    	return 0;
    }
    


    Output :



      Y


    In the above code we have initialised the variable x with the letter/alphabet Y.

    char x = 'Y';


    Now, the char data type before the variable x will make sure that no values other than letter/alphabet would be allowed in it.

    A char data type takes 1 byte of memory.

  3. Integers



    Integers are the whole numbers (i.e. 7 or 5 or 2.14).

    Below are the data types used for Integers.

    1. int Data Type



      The Data Type for a whole number is int.

      The Int data type is used to store whole numbers from -32,768 to 32,767. And takes 2 bytes of memory.

      Example :



      #include <iostream>
      using namespace std;
      
      int main() {
      
      	int x = -30535;
      	cout << x;
      	
      	return 0;
      }
      


      Output :



        -30535

    2. unsigned int Data Type



      The Data Type for unsigned number is unsigned in. i.e. unsigned int Data Type can hold positive numbers.

      The Int data type is used to store whole numbers from 0 to 32,767.

      And takes 2 bytes of memory.

      Example :



      #include <iostream>
      using namespace std;
      
      int main() {
      
      	unsigned int x = 30535;
      	cout << x;
      	
      	return 0;
      }
      


      Output :



        30535

    3. signed int Data Type



      The Data Type for signed number is signed int. i.e. unsigned int Data Type can hold positive and negative numbers.

      The signed int data type is used to store whole numbers from -32,768 to 32,767.

      And takes 2 bytes of memory.

      Example :



      #include <iostream>
      using namespace std;
      
      int main() {
      
      	signed int x = -30535;
      	cout << x;
      	
      	return 0;
      }
      


      Output :



        -30535

    4. long Data Type



      Say, we have a large value 3372036854775808.

      Well! In this case int data type will not be able to hold that value.

      And in this scenario, we need a long data type.

      And takes 4 bytes of memory.

      Example :



      #include <iostream>
      using namespace std;
      
      int main() {
      
      	long x = 3372036854775808;
      	cout << x;
      	
      	return 0;
      }
      


      Output :



        3372036854775808


      So, in the above example, we have created a variable x of type long. And assigned a large value 3372036854775808 to it.

    5. unsigned long int Data Type



      The Data Type for unsigned long int number is unsigned long int. i.e. unsigned long int Data Type can hold positive numbers.

      Example :



      #include <iostream>
      using namespace std;
      
      int main() {
      
      	unsigned long int x = 3372036854775808;
      	cout << x;
      	
      	return 0;
      }
      


      Output :



        3372036854775808

    6. short Data Type



      The short Data Type is used to hold whole numbers between -32,768 to 32,767.

      Say, there is a number i.e. 2768. So, to save space we can use Short Data Type.

      Example :



      #include <iostream>
      using namespace std;
      
      int main() {
      
      	short x = -2768;
      	cout << x;
      	
      	return 0;
      }
      


      Output :



        -2768

      Note : We can still use int with numbers 5 and 2768.

    7. unsigned short Data Type



      The unsigned short Data Type is used to hold whole numbers between 0 to 32,767.

      Say, there is a number i.e. 2768. So, to save space we can use unsigned short Data Type.

      Example :



      #include <iostream>
      using namespace std;
      
      int main() {
      
      	unsigned short x = 2768;
      	cout << x;
      	
      	return 0;
      }
      


      Output :



        2768

    8. Float Data Type



      A float data type is used to hold floating point numbers. And is used to hold the floating point numbers of 4 bytes.

      For example 5.987.

      Example :



      #include <iostream>
      using namespace std;
      
      int main() {
      	
      	float x = 5.987;
      	cout << x;
      	
      	return 0;
      }
      


      Output :



        5.987


      So, in the above example, we have created a variable x of type float. And assigned the value 5.987 to it.

      	float x = 5.987;

    9. double Data Type



      A double data type is used to hold floating point numbers. And is used to hold the floating point numbers of 64 bit.

      It is almost same as Float data type. Just that it can hold a floating point number of a larger size.

      For example 5.98743.

      Example :



      #include <iostream>
      using namespace std;
      
      int main() {
      	
      	double x = 5.98743;
      	cout << x;
      	
      	return 0;
      }
      


      Output :



        5.98743

  4. Booleans



    Boolean represents just two values, i.e. true or false.

    1. bool Data Type



      A bool Data Type can accept only true or false.

      Example :



      #include <iostream>
      using namespace std;
      
      int main() {
      	
      	bool x = true;
      	bool y = false;
      	
      	cout << x;
      	cout << '\n';
      	cout << y;
      	
      	return 0;
      }
      


      Output :



        1
        0


      So in the above example, we have initialised the variable x with value true,

      bool x = true;


      And variable y with false.

      bool y = false;


      Now, if you see the output, it is 0 and 1.

      So, internally what happens is, the value of false is converted to 0 and true to 1.