- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Static Variables in C
Static variables are initialized only once. The compiler persists with the variable till the end of the program. Static variables can be defined inside or outside the function. They are local to the block. The default value of static variables is zero. The static variables are alive till the execution of the program.
Here is the syntax of static variables in C language,
static datatype variable_name = value;
Here,
datatype − The datatype of variable like int, char, float etc.
variable_name − This is the name of variable given by user.
value − Any value to initialize the variable. By default, it is zero.
Here is an example of static variable in C language,
Example
#include <stdio.h> int main() { auto int a = -28; static int b = 8; printf("The value of auto variable : %d\n", a); printf("The value of static variable b : %d\n",b); if(a!=0) printf("The sum of static variable and auto variable : %d\n",(b+a)); return 0; }
Output
Here is the output
The value of auto variable : -28 The value of static variable b : 8 The sum of static variable and auto variable : -20
- Related Articles
- Class and Static Variables in C#
- Initialization of static variables in C
- Templates and Static variables in C++
- Where are static variables stored in C/C++?
- Default values of static variables in C
- Static variables in Java
- Initialization of global and static variables in C
- C++ static member variables and their initialization
- How static variables in member functions work in C++?
- Final static variables in Java
- What are the local static variables in C language?
- Static and non static blank final variables in Java
- When do function-level static variables get initialized in C/C++?
- Class or Static Variables in Python?
- Class and Static Variables in Java

Advertisements