- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a static storage class in C language?
There are four storage classes in C programming language, which are as follows −
- auto
- extern
- static
- register
Static variables
The keyword is static.
Scope
Scope of a static variable is that it retains its value throughout the program and in between function calls.
Static variables are initialised only once.
Default value is zero.
Example 1
Following is the C program for static storage class −
#include<stdio.h> main ( ){ inc ( ); inc ( ); inc ( ); } inc ( ){ static int i =1; printf ("%d", i); i++; }
Output
The output is stated below −
1 2 3
Example 2
Following is another C program for static storage class −
#include<stdio.h> main ( ){ inc ( ); inc ( ); inc ( ); } inc ( ){ auto int i=1; printf ("%d", i); i++; }
Output
The output is stated below −
1 1 1
Example 3
Following is the third example of the C program for static storage class −
#include <stdio.h> //function declaration void function(); int main(){ function(); function(); return 0; } //function definition void function(){ static int value= 1; //static variable declaration printf("
value = %d ", value); value++; }
Output
The output is stated below −
value = 1 value =2
Advertisements