C++ Data type and variable


There are four  data types in C++ :
  1. Integer data type
  2. Floating point data type
  3. Void data type
  4. Char data type
  5. Lateral  and constant

 1. Integar data type

An integer is an integral whole number without a decimal point. These numbers are used for counting. For example 26, 272, -342 are valid integers. Normally an integer can hold numbers from -32768 to 32767.

 2. Floating point Data Type

A floating point number has a decimal point. Even if it has an integral value, it must include a decimal point at the end. Valid floating point examples are 45.0, 34.23, -234.34.

  3. C++ Void Data Type

It specifies the return type of a function when the function is not returning any value.
It indicates an empty parameter list on a function when no arguments are passed.
A void pointer can be assigned a pointer value of any basic data type.

 4. C++ Char data type

It is used to store character value in the identifier (variable/operand).

Lateral and constant.

Any attempt to change the value of a constant will result in an error message.

A keyword const is added to the declaration of an identifier to make that identifier constant.

Syntax :

  const datatype variable = value;
Example :

  const float PI = 3.1415;
  const char ch = 'A';
  const int rate  = 40;

Variable declaration in c++

There is a important difference between C and C++ regarding when local variable can be declared. In c, you must declare all local variables used within a block at the start of that block. You cannot declare a variable in a block after an "action" statement has occurred.


Syntax :
 datatype variable_name;
Example :

// Incorrect in C. Ok in C++
int function()
{
    int num1;
    num1 = 10;
    /*
     won't compile as a C program
     but compiling as C++ program its OK
    */
    int num2;
    num2 = num1 * 5;

    return num2;
}

0 Comments