C Variables

c vaiables

What is Variables?

Variables is like a container. which are store the values. That is important part of programming language. when we Declare a variable,then compiler are allocates memory of this variable by according its data-type . the name of variable is an identifiers of this memory location. we are assign the value and access the value by variable name.

Syntax of variable declaration.

 data_type name_of_variable;

Example

/*
Example: variable declaration
*/
#include <stdio.h>
int main(){
  //Declaration of variable
  /*
    int data type
    number: name of variable
  */
  int number; 

  number=10;

  if(number>0){
    //code
  }else{
    //code
  }

  return 0;
}

Important points

1) Variable name are not keywords.
2) Data type of variable are user defined and basic (int,char so on).
3) Not need of any header file to define and declare of c variable.
4) Compiler are understand behaviour of variable by its name and data type when compile the program.

Phases of variables

Declaration : must before using variable is declared.

Initialization : Must be assign the variable value before the access. because we are not assign any value of variable. so by default its contain of garbage value in local variables.

Variables Phases

Access: utilize of variable value.

/*
Example: variable declaration,
     initialization and access  
*/
//header file for printf function
#include <stdio.h>

int main(){
  //Declaration and initilize variables
  int num1=2,num2=5,sum=0;
  
  //Operation of variable
  sum=num1+num2;
    
    //print sum= [num1+num2]
  printf("Sum : %d",sum); //7

  return 0;
}

Where Variables Are Declared

Variables declaration positions

Variables are declared of following positions.
[A] Inside a function

int main(){
  int age=21;
  float salary=10000.09f;
  char status='1';
}

[B] function parameter

void acount_info(int user_id);
void salary(int id);
void sum(int num1,int num2);
int main(int argc, char const *argv[])
{
  /* code */
  return 0;
}

[C] outside of function

int number=10;//global variable
int main()
{
  /* code */
  return 0;
}

Type of variable

Two types of variables

1) Local variables

2) Global variables

Type of C Variables

Local Variables: local variables are defined inside a function and within the block of function. Normally parameter of function also be a local variable.

/*
  Example local variable of function
*/
#include <stdio.h>
/*declare of function*/
void sum(int,int);

/*Sum of given two integer values*/
void sum(int num1,int num2){
  //local variable of num1 and num2
  printf("Sum is:%d",num1+num2);
}
int main(){
  //Start execution hear
  sum(10,20); //function calling
  return 0;
}

(num1 and num2) are local integer variables of sum function.

Local Variables

Output:

Sum is:30

Scope of Variable

There is an two types.Scope of variable are local and global

Variables Scopes

Local variables

Scope of local variable is within the defined block or declared function. Understand of this point using of this program

/*
  Example of scope of variable inside a function
*/
#include <stdio.h>
int main(){
  /*Entry point of program*/
    /*Outer int x variable*/
  int x=10;

  { /*Inner int x variable*/
    int x=20;
    printf("Inner variable x: %d",x);
  }
  //*Print outer x variables*/
  printf("\nOuter variable x: %d",x);

  return 0;
}

Output

Inner variable x: 20
Outer variable x: 10
Scopes Example

In above function has two integer variables [x]. Note that in same function more the two same variable are possible but there scope are different. Important point there are not possible to declare two same name variable in same scope of function.

Outer x variable scope are exist in inner block. So how to use outer x within inner block?. view this examples.

  
#include <stdio.h>
int main(){
  /*Entry point of program*/
    /*Outer x int variable*/
  int x=10;

  { 
    /*Print outer x variables*/
    printf("\nOuter variable x: %d",(x));
     /*Inner x int variable*/
    int x=20;
    printf("\nInner variable x: %d",x);
    

  }
  
  return 0;
}

Output

Outer variable x: 10
Inner variable x: 20

Note: Before declare inner x, use previous declare outer x.

Example of Global variables

/*
Example of global variables in c
*/
#include <stdio.h>
/*Global variables*/
int var1=10,var2=12;

int main(){
  /*Entry point of program*/

  /*Print global variable values*/
  printf("\nvar1 :%d",var1);
  printf("\nvar2 :%d",var2);

  return 0;
}

Output

var1 :10
var2 :12      
      

Important points

1) Global variable must be declare at head section of program.
2) Scope of global variable is until the program execution.
3) View default value of global variable in given program

/*
  Example of default values of global variables
*/
#include <stdio.h>
/*Global variables*/
int ivar;
float fvar;
char cvar;

int main(){
  /*Entry point of program*/

  /*Print global variable values*/
  printf("\nivar :%d",ivar);
  printf("\nfvar :%f",fvar);
  printf("\ncvar: %c",cvar );

  return 0;
}

Output

ivar :0
fvar :0.000000
cvar: 

4) Default value of global pointer is (NULL).

/*
  Example NULL value of global pointers
*/
#include <stdio.h>
//global integer ptr
int *ptr;
int main(){
  /*Check its value*/
  if(ptr==NULL){
    printf("NULL value of ptr pointer\n");
  }else{
    printf("Not NULL of ptr pointer\n");
  }
  return 0;
}

Output

NULL value of ptr pointer

Relationship of variable

We are declare variable of within function. so there are possible to have two different functions are same name of variables. view with example and clear this points.

/*
variable relations
*/
#include <stdio.h>
/*Function declaration*/
void fun1();
void fun2();
void fun3();

/*Function defination*/
void fun1(){
  /*Variable of function fun1*/
  int x=7;
  printf("\n %d",x);
  fun2();//call function
}
void fun2(){
  /*Variable of function fun2*/
  int x=8;
  printf("\n %d",x);
  fun3();//call function
}
void fun3(){
  /*Variable of function fun3*/
  int x=6;
  printf("\n %d",x);
}
int main(){
  /*Entry point of program*/
   fun1();
  return 0;
}

Output

 7
 8
 6
Differentiate variable

There are possible to declare constant variable. need to modified its syntax.

//const data_type name;
/*const: keyword
data_type: Data type
name: name of variables
*/
const float PI=3.14159f;

Example

/*
Constant variables
*/
#include <stdio.h>

//constant PI
const float PI=3.14159f;
int main(){
  /*Entry point of program*/
    printf("\nFloat : %f",PI);
  return 0;
}

Output

There are possible to find memory location of variable by its name.

/*
Find memory location
*/
#include <stdio.h>


int main(){
  /*Entry point of program*/
  int age=90;
    printf("\nLocation : %p",&age); //& operator use
  return 0;
}

Output

Location : 0x7fffe14476b4

Lvalues and Rvalues in C

Lvalues Rvalues

lvalue: lvalue is a memory locations. which are store some values. basically lvalue is a name of variable that are assign some values. important point lvalue are not applicable for constant variable. becaue they value are can not modified.

/*
  Example of lvalue
*/
#include <stdio.h>
int main(){
  int a,b;
  a=10; // (a is an lvalue) this are assign 10
  b=90; //(b is an lvalue) this are assign 90
  printf("%d %d\n",a,b);
}

Outout

10 90

Note that: a itself is also an lvalue.

  int x;//x is lvalue
  int *ptr;// ptr is lvalue

rvalue: In simple words rvalue is related to values. not in memory location. for example

  int x=42;//42 is an rvalue
  int y=23 //23 is an rvalue
  x=y;// y is rvalue and x is lvalue
  int *z=&x ; //*z is an lvalue. &x->memory locolation but in form of value

Static variables

Few important point of static variable.
1) Memory allocation of static variable only one time.
2) Default value is not of garbage. for normal variable contain garbage.
3) There are declare same as normal variable. only difference is append in front of (static).

Syntax of declare static variable

  static data_type variable_list;
  //static : keyword
  //data_type: data type of variable
  //variable_list: list of variables

  Example:
  static int num1,num2;
  static float fvar;
  static char cvar;
  //similar other c data variables

Example of static variable.

/*
    Example of Static variable
*/
//header file
#include <stdio.h>
// function declaration
void display();

void display(){
    static int auxiliary=10; //static variable
    printf("\n auxiliary variable is :%d",auxiliary); //print static variable
    auxiliary++;//increment by one
}
int main(){
    display();//function call
    display();// function call
}

Output

Static Variable Example
 auxiliary variable is :10
 auxiliary variable is :11

© 2019, regularcodes.com, All rights reserved