Wednesday 23 August 2017

Declaration of variables in C++ Language

Declaration of variables

int:

   This int keyword is used to declare integers, whole numbers either positive or negative. Most of the compilers treat this with a size of 2 bytes. i.e.,integer of 16 bits length. The following statement shows how the variables of int type are declared. 
     int var1;
     int var11 = 10; //Initialization for c++ tutorial

long:

   This long keyword is used for declaring longer numbers. i.e, numbers of length 32 bits.

float:

   This keyword float is used to declare floating point decimal numbers. A sample declaration would be, 
         float var2; //Sample declaration for float

int a;
float mynumber;
These are two valid declarations of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier mynumber. Once declared, the variables a and mynumber can be used within the rest of their scope in the program.
If you are going to declare more than one variable of the same type, you can declare all of them in a single statement by separating their identifiers with commas. For example:
int a, b, c;
This declares three variables (a, b and c), all of them of type int, and has exactly the same meaning as:
int a;
int b;
int c;
The integer data types char, short, long and int can be either signed or unsigned depending on the range of numbers needed to be represented. For Example-

unsigned short int NumberOfSisters;
signed int MyAccountBalance;




short Year;
short int Year;

unsigned NextYear;
unsigned int NextYear;

// operating with variables
 
#include <iostream>
using namespace std;
 
int main ()
{
  // declaring variables:
  int a, b;
  int result;
 
  // process:
  a = 5;
  b = 2;
  a = a + 1;
  result = a - b;
 
  // print out the result:
  cout << result;
 
  // terminate the program:
  return 0;
}
4

char data types:

char:

   This keyword is used to declare characters. The size of each character is 8 bits. i.e., 1 byte. The characters that can be used with this data type are ASCII characters.

string:

   This is not a basic data type with C++. Instead array of the char data type should be used for a string variable. 
     char strvar1[200];
     char strvar2[]="string can also be initialized like this for the C++ Tutorial"; 


No comments:

Post a Comment