C++ Variable : Identifier declare variable

variable define, declare and what are those?

By all means C++ is a statically typed language. The type also indicates nature or category of the variable that would store data at a memory location in your computer for further manipulation. There fore you can't use any variable without specifying its type. By defining a variable in C++ you are avoiding data corruption, like a word can't be replaced with an integer, along that line. The same way variables that will have decimals and long number or string need to be defined by their corresponding type.

  • defining a variable
    • data_type identifier
      • int   _amount  or string str or char ch
      • where int is the data type and _amount is an identifier.
        • you may identify the variable with alpha-numeric values like n1, i1,x1 or just a character like x or i. You can't use numerals like 1 as an identifier.
    • Initialize: variable must be initialized before use: meaning you must assign some value associated with the data type
      • n1 = 1234;
      • ch ='A';
      • str = "what is your name"
  • define and declaration in one shot
    • int _amount = 10;
    • int _amount = 'A'; is wrong
    • char c = '1'; //is OK when you treat as a character
    • string str = "1234"; //is ok when you treat as a string , not as a number.
Example

#include <iostream.h>
using namespace std;

//varioable_5.cpp
int main()

{
cout << "define and declaring variables : data types\n ";
double hours, rate, total; //defining variable as double type
//in C++ you have declare a variable before you can use it.
//defined variable has only the signature of a variable,
//and not the value attached with it
hours = 40.25 ; // declaring or intializing a variable
rate = 70.25; // declaring or intializing a variable
total = hours * rate;// declaring or intializing a variable
cout << "week salary " << total <<endl ;
double d2 = total; // data transfer within similar type
int round_up = total; //conversion of one data type to others with a loss
cout << "week salary round up " << round_up << " original " << d2 ;
return 0;

}