Users Online

· Guests Online: 109

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Local Variable in C

Local Variable in C

 

Updated April 3, 2023

Local Variable in C

Introduction to Local Variable in C

The following article provides an outline for Local Variable in C. Local variable is defined inside the function or in the block, and it should be declared at the start of the function. A local variable must have been initialized before it is to be used. Local variables use only by the statements which are inside the function or block of code. The local variable does not know if it is declared outside the function.

Syntax:

void sample_1()
{
int a=10; // local variable declaration
}

A local variable must have been initialized before it is to be used.

How Local Variable Works in C?

A local variable is declared within the function or program block, and it can be used inside the code block or subroutine in which it’s declared. Local variable exists until the code of block execution; once the code executes, it destroys automatically.

Example:

Code:

public int multiply ()
{
int x=2; int y=5;
return x*y;
}

In the above code, the local variables are x and y it declared only within the function multiply().

Local variables are declared inside the function, and those variables are useless when the control of the program reaches outside of the block. Local variables are accessed using the block scope and the lifetime of those local variables are until the function or block of code. A local variable is declared inside the function or in the block, and it should be declared at the start of the function.

Example:

Code:

void method_1()
{
int x,y;
}
void method_2()
{
printf("%d\n",x); // it raises ERROR, in method_2() because it does not know any variable x
}

In the above simple program, x and y are the local variables; those variables are available only inside the function call in which they are defined in method_1(). If you use those variables outside of the function, you will get an error. This is because those variables are available only the function method_1() is executing, and those variables are destroyed once the function method_1() ends.

Examples of Local Variable in C

Given below are the examples of Local Variable in C:

Example #1

Code:

#include <stdio.h>
int main()
{
int value1 = 20;
int value2 = 15;
printf("\tValue 1 is %d\n\tValue 2 is %d\n", value1, value2);
{
// value1 and value2 are in the inner block
int totalValue = value1 + value2;
printf("\nThe Total Value is %d\n", totalValue);
} // here totalValue is declared inside the block
// printf("totalValue = %d\n", totalValue); // it raises ERROR, because it does not known any variable totalValue
return 0;
}

Output:

Local Variable in C 1

Example #2

Code:

#include <stdio.h>
/* outside declaration - global variable */
int a;
int main () {
/* inside declaration - local variable */
int x, y;
/* initializing the variables */
x = 10;
y = 20;
a = x + y;
printf("EXAMPLES OF VARIABLES:\n");
printf ("Value of x = %d\nValue of y = %d\nValue of a = %d\n", x,y,a);
return 0;
}

Output:

Local Variable in C 2

Example #3

Code:

#include <stdio.h>
/* outside - global variable declaration */
int a = 25;
int main () {
/* inside - local variable declaration */
int a= 15;
printf("Output\n");
printf ("Value of a = %d\n", a); // output displays only the inner function call a
return 0;
}

Output:

a = 15

Global and Local Variables

Local variables are declared inside the function, and those variables are useless when the control of the program reaches outside of the block. This is because local variables are accessed using the block scope and the lifetime of those local variables are until the function or block of code, whereas global variables are declared outside all functions.

Example:

Code:

#include<stdio.h>
// Outside declaration is a Global variables
int num1;
int num2;
int Addition()
{
return num1 + num2;
}
int main()
{
int result; //declaring inside function called Local variable
num1 = 16;
num2 = 4;
result = Addition(); // it calls the Addition() function
printf("Example for Local Variables\n");
printf("Result: %d\n",result);
return 0;
}

In the above code, two variables are declared num1 and num2; those variables are used in the Addition() and main() function, but it finally displays the result, which is only used in the main() function.

Output:

Result: 20

Local variables are declared inside the function, and those variables are useless when the control of the program reaches outside of the block. This is because local variables are accessed using the block scope and the lifetime of those local variables are until the function or block of code. In contrast, global variables are declared outside all functions, whereas the variables in global in which it is declared can be used everywhere in the program. They can be acceded and modified in any function of the program.

Example:

Code:

#include <stdio.h>
/*outside declaration - global variables*/
int value1,value2;
/*function to set values to the global variables*/
void DisplayValues(void)
{
value1=25;
value2=35;
}
int main()
{
/*inside declaration - local variables*/
int a,b;
a=50;
b=70;
DisplayValues();
printf("Output\n\n");
printf("Global Variables:\nValue-1:%d,Value-2:%d\n",value1,value2);
printf("Local Variables:\na=%d, b=%d\n",a,b);
return 0;
}

Output:

Local Variable in C 5

In the above code using both global variable (value1,value2) and local variables (a,b) are used respectively. The global variables are accessed anywhere of the program, so they are using within function DisplayValues() and main(). Where a and b are used only with the main() function because they are local variables declared only within the main function(), so it can be only accessed with the main() function.

Try to minimize the variable usage with the same name in the outside and inside block of code because it’s difficult to trace and read errors. It’s good to initialize local variables in before use to override their garbage value. For a good programmer, have the practice to initialize every variable correctly or else our program will build with unexpected errors to avoid this make initializing the variables because it causes uninitialized variables to garbage value at the memory location.

Conclusion

In this article, we have seen about the Local Variables in C explained with several examples programmatically; it’s good practice to use local variables correctly at the initial stage of programming because by using like this, we can avoid making errors in our programs.

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.93 seconds
10,267,474 unique visits