Skip to main content

C Variables

Variable is an memory holder, that is very important part of every programming language. There are few important point you are very important to know, when we define a variable to in our code and compile the source code. Then complier are allocates memory of variable by using of its defining data type.

C Vaiables

Variable is an identifier, we are use this allocated memory by storing value of variable by its name. There is possible to set new value and also get stored value by its name. There is few special case we are need to create immutable constant variable. That is also possible to create this type of variables.

We can defined variable by using of this syntax.

data_type variable_name;

Define of variable in c programming are very easy. First we are decided what kind of data will stored by variable . According to values provide the data type of variables. For xample

/*
  Example: Declaration of variable
*/
#include <stdio.h>
int main(){
  //Declaring an integer variable
  int number; //number is name of variable and int data type
  number=10; //Assign integer value
  printf("%d",number);//display value
  return 0;
}
Output
10

In this example number is name of variable and this are capable to stored integer values. We can declare similar type of multiple variable in single statement.

data_type varible1,variable2;

Important points

1) Name of variable is an identifier, we cannot use keyword as variable name.

2) We can used any kind of data type to defined variable. such as predefined and user defined (union ,enum ,struct etc).

3) There are no header file required to define variable.

4) Memory of variable is always Allocated using of define variable data type.

5) Every variable are unique address, That are system define.

How To use variables

There is basically three phase. first declare of variable and assign some values, after that access variable value in on program.

Variables Phases

Declaration: Declaration means specified the behaviour of variable, such as what kind of value is store by variable, what is the scope of variable, and which kind of memory that are need.

Initialization : is a process to assign the value of variables. this is very important are assigning of a valid value to variables. Because when we are not provide any value to variable then that will take default values. and in normal case default value of variable is garbage. There are some special cases that variable value are not be garbage. we will discuss on this cases in next section.

Access: is an operation, like that modify variable value and used of variable value. let take an simple example to demonstrate following situations.

/*
  Example: Variable declaration,
  initialization and access  
*/
//header file for printf function
#include <stdio.h>
int main(){
  //Declaration of multiple variables 
  //data type is integer
  int num1=2,num2=5,sum=0; 
  //Operation of variable
  sum=num1+num2;

  //print sum= [num1+num2]
  printf("Sum : %d",sum); //7
  return 0;
}
Output
7

Where Variables Are Declared

C variable are declared anywhere within the program. this declaration is decides scope and access of variable.

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).

4) Lifetime of this variable is until on program execution. but its accessibility is depending upon where is this defined.

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
    //print static variable
    printf("\n auxiliary variable is :%d",auxiliary); 
    auxiliary++;//increment by one
}
int main(){
    display();//function call
    display();//function call
}

Output

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




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment