- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Static Keyword in C
In C language, the "static" keyword is used to declare a static variable as well as to define a static function. When declared as "static", a variable represents a static storage class.
A static function is available only inside the program file (with ".c" extension) in which it is defined. One cannot use the "extern" keyword to import a static function into another file.
Static Variable in C
When a variable is declared as static, it is initialized only once. The compiler persists with the variable till the end of the program. A static variable is also used to store data that should be shared between multiple functions.
Here are some of the important points to note regarding a static variable −
- The compiler allocates space to the static variable in computer’s main memory.
- Unlike auto, a static variable is initialized to zero and not garbage.
- A static variable is not re-initialized on every function call, if it is declared inside a function.
- A static variable has local scope.
Example
In the following example, the variable "x" in the counter() function is declared as static. It is initialized to "0" when the counter() function is called for the first time. On each subsequent call, it is not re-initialized; instead it retains the earlier value.
#include <stdio.h> int counter(); int main() { counter(); counter(); counter(); return 0; } int counter() { static int x; printf("Value of x as it enters the function: %d\n", x); x++; printf("Incremented value of x: %d\n", x); }
Output
When you run this code, it will produce the following output −
Value of x as it enters the function: 0 Incremented value of x: 1 Value of x as it enters the function: 1 Incremented value of x: 2 Value of x as it enters the function: 2 Incremented value of x: 3
A static variable is similar to a global variable, as both of them, are initialized to 0 (for numeric types) or null pointers (for pointers) unless explicitly assigned. However, the scope of the static variable is restricted to the function or block in which it is declared.
Static Function
By default, every function is treated as global function by the compiler. They can be accessed anywhere inside a program.
When prefixed with the keyword "static" in the definition, we get a static function that has a scope limited to its object file (the compiled version of a program saved with ".c" extension). This means that the static function is only visible in its object file.
A static function can be declared by placing the keyword "static" before the function name.
Example 1
Open a console application with the CodeBlocks IDE. Add two files "file1.c" and "main.c". The contents of these files are given as follows −
Contents of "file1.c" −
static void staticFunc(void) { printf("Inside the static function staticFunc() "); }
Contents of "main.c" −
#include <stdio.h> #include <stdlib.h> int main() { staticFunc(); return 0; }
Now, if the above console application project is built, then we will get an error, i.e., "undefined reference to staticFunc()". This happens as the function staticFunc() is a static function and it is only visible in its object file.
Example 2
The following program demonstrates how static functions work in a C program −
#include <stdio.h> static void staticFunc(void){ printf("Inside the static function staticFunc() "); } int main(){ staticFunc(); return 0; }
Output
The output of the above program is as follows −
Inside the static function staticFunc()
In the above program, the function staticFunc() is a static function that prints "Inside the static function staticFunc()". The main() function calls staticFunc(). This program works correctly as the static function is called only from its own object file.
Example 3
You can have multiple static functions in the same object file, as illustrated in the following example −
#include <stdio.h> #include <stdlib.h> #include <math.h> // define the static function static int square( int num){ return num * num; } static void voidfn(){ printf ("From inside the static function.\n"); } static int intfn(int num2){ return sqrt(num2); } int main(){ int n1, val; n1 = 16; val = square(n1); // Call voidfn static function printf("The square of the %d : %d\n", n1, val); voidfn(); // Call intfn static function val = intfn(n1); printf("The square root of the %d : %d\n", n1, val); return 0; }
Output
When you run this code, it will produce the following output −
The square of the 16: 256 From inside the static function. The square root of the 16: 4