- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Variables

Posted by Tayyab


Definition:
                            A quantity whose value may change during execution of the program is called variable. It is represented by a symbol or a name.
Explanation:
                  A variable represents a storage or memory location in the computer memory. Data is stored into the memory location. i.e. the variable name, remains fixed during execution of the program but the data stored in that location may change from time to time.
               A variable is also known as object in C++. In C++, a variable name consists of alphabets and digits.
Rules for Writing Variables Names OR Identfier:
            The following are the rules for writing a variable name in a program in C++:
                  1 - The first character of variable name must be an aphabetic character.
                  2 - Underscore can be used as first character of variable name.
                  3 - Blank spaces are not allowed in a variable name.
                  4 - Special characters, such as arithmetic operators, #, ^, cannot be used in a variable name.
                  5 - Reserved words cannot be used as variable names.
                  6 - The maximum length of a variable name depends upon the compiler of C++.
                  7 - A variable name declared for one data-type cannot be used to declare another data-type.

C++ is a case-sensitive language. Thus variable names with same spellings but different cases are treated as different variable names. For example, variables 'Pay' and 'pay' are two different variables.

Following are some examples of the valid and invalid variable names:

Variable NameValid / InvalidRemarks
TayyabValid
performValid
doubleInvalid
C++ reserved Word
foxpro
Valid

switch
Invalid
C++ reserved Word
Ali
Valid

int
Invalid
C++ reserved Word
3taq
Invalid
Starts with a numerical
unsigned
Invalid
C++ reserved Word
x-y
Invalid
Special character is not allowed
Taq Ahd
Invalid
Space is not allowed
char
Invalid
C++ reserved Word
cout
Invalid
C++ reserved Word

Variable Declaration in C++:

A variable declaration provides assurance to the compiler that there is one variable existing with the given type and name so that compiler proceed for further compilation without needing complete detail about the variable. A variable declaration has its meaning at the time of compilation only, compiler needs actual variable declaration at the time of linking of the program.
A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program. You will use extern keyword to declare a variable at any place. Though you can declare a variable multiple times in your C++ program, but it can be defined only once in a file, a function or a block of code.
Some valid declarations are shown here:
int    i, j, k;
char   c, ch;
float  f, salary;
double d;
The line int i, j, k; both declares and defines the variables i, j and k; which instructs the compiler to create variables named i, j and k of type int.
Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows:
type variable_name = value;
Some examples are:
extern int d = 3, f = 5;    // declaration of d and f. 
int d = 3, f = 5;           // definition and initializing d and f. 
byte z = 22;                // definition and initializes z. 
char x = 'x';               // the variable x has the value 'x'.
For definition without an initializer: variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables is undefined.

Example

Try the following example where a variable has been declared inside the main function:
#include <iostream.h>
  
int main ()
{
  // Variable definition:
  int a, b;
  int c;
  float f;
 
  // actual initialization
  a = 10;
  b = 20;
  c = a + b;
 
  cout << c << endl ;

  f = 70.0/3.0;
  cout << f << endl ;
 
  return 0;
}
When the above code is compiled and executed, it produces the following result:
30
23.3333

Lvalues and Rvalues:

There are two kinds of expressions in C++:
  • lvalue: An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.
  • rvalue: An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.
Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side. Following is a valid statement:
int g = 20;
But following is not a valid statement and would generate compile-time error:
10 = 20;


<< Previous                                                                                                                 Next >>    

0 comments:

Post a Comment