VARIABLES in c

In this blog
variables 
Rule for forming variables 
Definiton of variables
Declaration of variables
Lvalues and rvalues with eg
 

 
variables 

a variable  in is a name given to storage location  so that you can easily access the value at that location or assignee the value , also from the name you can tell that it's value can be varied ( you can change the value to that location using that variable name) ,by using variable it become eassier to access that location as you don't had to remember the address to the location.


Rules for  forming variables

rules for forming variables are same as that of identifier in case you don't no you can refer to 


Definition of variables

A definition of a variable tell the compiler to allocate the memory and give the  specified variable name to it and of given data type .


eg:
                                  


data_type can be any valid c data type (  basic , derived , user defined ). you can also  define several variables in one definition statement 

eg:        

  
a real eg: 
                               

compiler will allocate the memory and assignee the name a1 , a2 ,a3   . like 

                           (note : address can be different but they would be in sequence) 

Declaration of variables

Declaration  of  variable  tells  the  compiler  that  there  exsist  the  variable  with  specific variable  name  and  with  specific  data  type  that  we  are  gonna  use  it  in  the  program later  on,  the  compiler  would  not  reserve any space in the memory  for  that  declaration .

to declare a variable we had to use the keyword extern ,eg:   


eg of code:
 

in this we had used var declaration and defintion in single file but its actual use come when 
we had to use  a single variable in 2 or more different files  .

(to check out eg in deffrent file        )
         
                    (note : for using the variable in diffrent file the defintion of variable should be global)

    
Lvalues and Rvalue

Lvalue :-lvalues are basically those value which is pointing to some address in memory , so that you can assignee something to it or you can  take the value it is pointing to , the basic meaning of lvalue is the value to the left side of assignment operator but you can use lvalue to both side of assignment operator


eg of lvalues:

1) A variable with some addres 

2) A constant variable       (only at time of initialization )

3) A structure variable    (struct1=struct2)  (where struct1 and struct2 are 2 structure variable of same data type)
 
4) A structure member                          (struct.member1 =2) (member1 is a member of  that structure) 

5) A array with particluar index                                                ( arr[0] , arr[2] ....etc)

6) A character pointer                                                           (char *p="programi boy")

7) A derefrencing pointer                                       ( *a =2) (where a is integer pointer)

8) A union variable or its member                          ( same as of struture)

and many more....
                                      (these value cant act both as lvaue and rvalue)

Rvalue:- Rvalue refer to value , so you can give that to some variable and basic meaning of rvlue is that value to right side of assignment operator ,you can use rvalue as lvalue but not every time

eg of rvalues that can act as lvalue aslo: 

1) a=b        
    c=a             
(where a and b are variables with some values and a is acting as both lvalue and rvalue)

2) int *a =&b
     int c=*a
(where *a is pointer to int  and is acting as lvalue and rvalue)

eg of rvalues that cannot act as lvalue aslo: 

1)other than that you can write expression    
2) A function call                   (int a= fun() ) (where the return type of function fun is int)

and many more.....

back to top

(leave the comment down below if you have a doubt or suggestions ,thnks🙂)

No comments:

Post a Comment

Contact Form

Name

Email *

Message *