Thursday, January 17, 2019

Data types and variables in c++ programming

in this post, you will learn about the data types and variables in c++ programming. and learn how to declare a variable in programming

data types and variables in c++ programming, learn c++ programming
learn c++ programming

Data types and variables

Data types:

In c++ programming there are mainly 8 types of data types available.
  • integer.
  • floating.
  • character.
  • void.
  • long.
  • double.
  • bool.
  • short.

Note: long keyword is used with the integer variable to increase the bit memory space, so the long keyword store the 4-bit memory space. and the double keyword is used with the float type variable so the double keyword stores the 8-bit memory space.

Integer data type:

In integer type of variable, we only store the numeric values, like 1, 2, and 10. and it's store the 2 bit of memory space.

How to declare an integer variable:


we use int keyword to declare an integer variable.

syntext:
            <int> <variable-name>;

Example:
                int a; // declare a integer variabel name

Note: the range of integer variable is from -32,768 to 32,767.

long int:
long int also stores the numeric values, but it takes the 4 bit of memory space to store the value. so we use the long int variable to store the high range of integer values.

syntext:
             <long int> <variable-name>;

Example:
               long int a; // declare a long integer type variable name a


short int:
short int also stores the numeric values, and also take the 2 bit of memory space to store the values. but it's used for store the short range of integer values.

syntext:
            <short int> <variable-name>;

Example:
               short int a; // declare a short integer variable name a


Float data type:

In the floating type of variables, we can only store the floating type of values, like 1.11, 2.5 etc. and it's store the 4 bit of memory space.

declaration of float variable:
we use the float keyword to declare a floating variable.
                          
syntext:
             <float> <variable-name>;
                          
Example:
              float a; // declare a float variable
       
Note: the range of floating type variables is started from -3.4e-38 to 3.4e+38.


double float:
it also stores the floating type of values. but it stores the 8 bit of memory space. and the range of double float variable is start form -1.7E+308 to +1.7E+308.

syntext:
            <double float> <variable-name>;

Example:
               double float a; //declare a double float variable name a

character data type:

character variable store the character values, like a, b, c etc. it only stores the one-bit memory space. and the range of character variable starts from -128 to 127.

declaration of character variable:
we use char keyword to declare a character variable.

syntext:
            <char> <variable-name>;

Example:
               char a; // declare a character variable name a

void data type:

void data type has not stored any type of values and it stores the 0-bit memory space.

declaration of a void variable:
we use the void keyword to declare a void variable.

syntext:
             <void> <variable-name>;

Example:
               void a; // declare a void variable name a

bool data type:

bool data type has only two values, 1(true) and 0(false).

declaration of bool variable:
we use bool keyword to declare a boolean variable.

syntext:
            <bool> <variable-name> = true(1)/false(0);

Example:
               bool b=true; // true means b has store the value 1
               bool b=false; // false means b has store the value 0

Signed and unsigned variable:

signed variables:
signed variables store both values positive and negative. and by default, all the variable has signed variables.

Example:
                int a;
                float f; 
                char c;
Note: we need not use signed keyword.


unsigned variables:
unsigned variables store only positive values. and to declare an unsigned variable we need to use the unsigned keyword.

syntext:
            <unsigned> <data-type> <variable-name>;

Example:
               unsigned int a; //now we can only store the positive integer values

Continue learning:
"Please share this knowledge as much possible as you can and also comment me your queries and question related to this topic in the section given below. "

0 comments: