
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What are the local static variables in C language?
A local static variable is a variable, whose lifetime doesn’t stop with a function call where it is declared. It extends until the lifetime of a complete program. All function calls share the same copy of local static variables.
These variables are used to count the number of times a function is called. The default value of static variable is 0. Whereas, normal local scope specifies that the variables defined within the block are visible only in that block and are invisible outside the block.
The global variables which are outside the block are visible up to the end of the program.
Example
Following is the C program for local variable −
#include<stdio.h> main ( ){ int a=40 ,b=30,sum; //local variables life is within the block printf ("sum=%d" ,a+b); }
Output
When the above program is executed, it produces the following output −
sum=70
Example
Following is the C program for global variable −
int c= 30; /* global area */ main ( ){ int a = 10; //local area printf ("a=%d, c=%d", a,c); fun ( ); } fun ( ){ printf ("c=%d",c); }
Output
When the above program is executed, it produces the following output −
a =10, c = 30
Example
Following is the C program for the local static variable −
#include <stdio.h> void fun(){ static int x; //default value of static variable is 0 printf("%d ", a); a = a + 1; } int main(){ fun(); //local static variable whose lifetime doesn’t stop with a function call, where it is declared. fun(); return 0; }
Output
When the above program is executed, it produces the following output −
0 1
- Related Articles
- Are static local variables allowed in Java?\n
- What are local variables in C++?
- What are local variables and global variables in C++?
- What are the local and global scope rules in C language?
- What are class variables, instance variables and local variables in Java?
- What are Local Scope Variables in Postman?
- Where are static variables stored in C/C++?
- Static Variables in C
- Final local variables in C#
- What are the rules for local and global variables in Python?
- Global and Local Variables in C#
- What is a static storage class in C language?
- What is a structure at local scope in C language?
- Initialization of static variables in C
- Templates and Static variables in C++
