- 2067 Asar · 2 marks
Syntax of static
Short answer
static data_type variable_name = value; makes a local variable persist across function calls, initialized only once and defaulting to zero. It retains its value for the program's lifetime while remaining locally scoped.
Write the syntax used in C programming language for static.
static is a storage class. A static local variable is created only once, keeps its value between function calls and exists for the whole run of the program, although it is visible only inside its block. It is initialised only once, and to 0 by default. (A static global variable or function is visible only within its own source file.)
Syntax: static data_type variable_name = value;
#include <stdio.h>
void counter(void)
{
static int count = 0; /* initialised once, keeps value between calls */
int normal = 0;
count++;
normal++;
printf("static = %d, auto = %d\n", count, normal);
}
int main(void)
{
counter();
counter();
counter();
return 0;
}
Output:
static = 1, auto = 1
static = 2, auto = 1
static = 3, auto = 1
The static variable keeps counting (1, 2, 3), whereas the normal local variable starts again at 0 in every call.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Define programming language · Computer Programming, chapter 1 · asked 3×
- Syntax of while loop · Computer Programming, chapter 5
- Syntax of if...else · Computer Programming, chapter 5
- Syntax of struct · Computer Programming, chapter 8
- Formatted I/O in FORTRAN with syntax · Computer Programming, chapter 11
- I/O functions used with data files · Computer Programming, chapter 10 · asked 2×
- Comparators used in static relays · Switchgear and Protection, chapter 8 · asked 2×
- 8086 assembly language operators · Microprocessor, chapter 3 · asked 3×
- Syntax of operator overloading · Object Oriented Programming, chapter 5 · asked 4×
- Class and function template with syntax · Object Oriented Programming, chapter 9 · asked 4×