Friday, 24 June 2016

VARIABLE AND CONSTANTS


VARIABLE AND CONSTANTS





Variable:-                                                            

                    Variable is data name that are stored in the memory. A variable have two values that is rvalue and lvalue. The Meaning of rvalue is the real value of the variable and the lvalue is the location value of the variable where the variable is saved.

Example:- a=30,b=54,x,y,z;

          100                    102                      104                               106                     108
       30

             54


          a                         x                        b                                 y                             z
here ‘a’ is a variable
 rvalue of a = 30
 lvalue of a =100   
       same as
 rvalue of b = 54
 lvalue of b = 104.




Declaration:-

                     If you use a variable in C , we must first declare it specifying which data type we want it to be. The syntax to declare a new variable is to write the specifies of the desired data type- followed by a valid variable identifier.

For example:          Data type variable_name;

                               int x;

                               float myid;

These are two valid declarations of variables. In first example declares a variable of type int with the identifier x and the second one declares a variable of type float with the identifier myid. Once declared, the variable x and myid can be used within the rest of their scope in the program.

If you are want to declare more than one variable of the same type, you can declare all of them in a single statement by separating with commas.

For Example:-                    int x,y,z;



Initialization:-

                           When declaring a regular local variable, its value is undetermined by default. But if you want a variable to store a concrete value at the same moment that it is declared. In order to do that, you can initialize the variable.

Syntax-    type identifier= initial_value;



Example int x=0;







Constants:-

               Constants are the expression with the fixed value which does not changes during the execution of the program.

  1. Integer Numerals- Numerical constants that identify integer constant.
    int a=2;
  2. Floating point numbers- decimals
    Float e=28.3;
  3. Character and string literals- non-numerical constants,
    ‘d’
    ‘g’
    “Hello world”
    “How you feel know?”


Note:-

   The first two expressions in ‘ ’ represent the single character and other two expressions in “ “ represent the string literals bundle  of several character.

             If you want to represent a single character than we us single quote (‘) and when we express a string than we use double quote (“).





Declared constants (const) :-

    With the ‘const’ prefix you can declare constants same as the variable.

Syntax- const type identifier= initial_value;



Example- const int p=5;

                const char ch=’y’;

 Here the value of p=5 is fixed for whole program, it doesn’t modified while execution. Same as ch.



                                        Thank you




No comments:

Post a Comment