06. C – Variables

By | October 24, 2020

The naming of an address is known as a variable. Variable is the name of a memory location. Unlike constant, variables are changeable, we can change the value of a variable during the execution of a program. A programmer can choose a meaningful variable name. Example: average, height, age, total, etc.

Variable Declaration:

A typical variable declaration is of the form:

  type variable_name;
    or for multiple variables:
  type variable1_name, variable2_name, variable3_name;

A variable name can consist of alphabets (both upper and lower case), numbers and the underscore ‘_’ character. However, the name must not start with a number.

Difference b/w variable declaration and definition:

The variable declaration refers to the part where a variable is first declared or introduced before its first use. A variable definition is a part where the variable is assigned a memory location and a value. Most of the time, variable declaration and definition are done together.

See the following C program for better clarification:

#include <stdio.h>
int main()
{
// declaration and definition of variable 'a123'
char a123 = 'a';   
// This is also both declaration and definition as 'b' is allocated
// memory and assigned some garbage value.   
float b;    
// multiple declarations and definitions
int _c, _d45, e;   
// Let us print a variable
printf("%c \n", a123);  
return 0;
}

Output:

a

Is it possible to have a separate declaration and definition?
It is possible in the case of extern variables and functions. See question 1 of this for more details.

Lvalues and Rvalues in C

There are two kinds of expressions in C −

  • lvalue − Expressions that refer to a memory location are called “lvalue” expressions. An lvalue may appear as either the left-hand or right-hand side of an assignment.
  • rvalue − The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.

Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the following valid and invalid statements −

int g = 20; // valid statement

10 = 20; // invalid statement; would generate compile-time error


Rules for defining variables

  1. A variable can have alphabets, digits, and underscore.
  2. A variable name can start with the alphabet, and underscore only. It can’t start with a digit.
  3. No whitespace is allowed within the variable name.
  4. A variable name must not be any reserved word or keyword, e.g. int, goto , etc.

Types of Variables in C

1. Local Variable
A variable that is declared and used inside the function or block is called a local variable. Its scope is limited to function or block. It cannot be used outside the block. Local variables need
to be initialized before us.
Example –

#include <stdio.h>
void function() { int x = 10; // local variable }  
int main() { function(); }

In the above code x can be used only in the scope of function() . Using it in main function will give error.

2. Global Variable
A variable that is declared outside the function or block is called a global variable. It is declared at the starting of the program. It is available to all the functions.
Example

#include <stdio.h>
int x = 20;//global variable
void function1()
{ printf("%d\n" , x); }
void function2() { printf("%d\n" , x); }
int main() {  function1(); function2(); return 0; }

Output

20
20

In the above code both the functions can use global variable x as we already global variables are accessible by all the functions.

3. Static Variable
A variable that retains its value between multiple function calls is known as a static variable.
It is declared with the static keyword.
Example-

#include <stdio.h>
void function(){  
int x = 20;//local variable  
static int y = 30;//static variable  
x = x + 10;  
y = y + 10;  
printf("\n%d,%d",x,y);  
}  
int main() {  
function();
function();
function();
return 0;
}

Output

30,40
30,50
30,60

In the above example , local variable will always print same value whenever function will be called whereas static variable will print the incremented value in each function call.

4. Automatic Variable
All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using the auto keyword. Automatic variables are similar to local variables.
Example

#include <stdio.h>
void function() {
int x=10;//local variable (also automatic)  
auto int y=20;//automatic variable
}
int main() {  
function();
return 0;
}

In the above example both x and y are automatic variables .The only difference is that variable y is explicitly declared with auto keyword.

5. External Variable
The external variable can be shared between multiple C files. We can declare an external variable using the extern keyword.

Example:

  myfile.h

  extern int x=10;//external variable (also global)  

   
  program1.c
  #include "myfile.h"  
  #include <stdio.h>  
  void printValue(){  
  printf("Global variable: %d", global_variable);  
  }  

In the above example x is an external variable which is used in multiple files.

Difference between Variable and Identifier

An Identifier is a name given to any variable, function, structure, pointer or any other entity in a programming language. While a variable, as we have just learned in this tutorial is a named memory location to store data which is used in the program.

IdentifierVariable
Identifier is the name given to a variable, function etc.While, variable is used to name a memory location which stores data.
An identifier can be a variable, but not all indentifiers are variables.All variable names are identifiers.
Example:// a variable int studytonight; // or, a function int studytonight() { .. }Example:
// int variable
int a;
// float variable float a;

Leave a Reply

Your email address will not be published. Required fields are marked *