Sunday, August 25, 2013

Variables

// Variables

Rules of Declaring Variables in C++
  • Variable name can consist of letter, alphabets and start with underscore character.
  • First character of variable always should be alphabet and cannot be digit.
  • Blank spaces are not allowed in variable name.
  • Special characters like #, $ are not allowed.
  • A single variable can only be declared for only 1 data type in a program.
  • Upper and lowercase letters are distinct because C++ is case-sensitive, so if we declare a variable name and one more NAME both are two different variables.
  • C++ has certain keywords which cannot be used for variable name.
  • A variable name can be consist of 31 characters only if we declare a variable more than 1 characters compiler will ignore after 31 characters.
The following is a table of the names of the types of variables:
TypeDescription
boolStores either value true or false.
charTypically a single octet(one byte). This is an integer type.
intThe most natural size of integer for the machine.
floatA single-precision floating point value.
doubleA double-precision floating point value.
voidRepresents the absence of type.
wchar_tA wide character type.


Example 1:

#include <stdio.h>
int a=5;
int b=2;
void add()
{
int sum;
sum = a + b;
printf("Sum of a and b is %d\n",sum);
}
int main()
{
add();
return 0;
}


//Video Tutorial





Example 2:

#include <iostream>
using namespace std;
int a=5;
int b=2;
void add()
{
int sum;
sum = a + b;
cout <<"Sum of a and b is "<<sum<<"\n";
}
int main()
{
add();
return 0;
}

1 comment: