---
title: "Syntax of static"
subject: "Computer Programming"
chapter: 6
key: "semester_1/computer_programming:syntax-static"
source: https://www.bctengineer.com/q/semester_1/computer_programming/syntax-static
site: BCT Engineering
---

# Syntax of static

Computer Programming · Chapter 6: User-Defined Functions

- Asked in: 2067 Asar, 2 marks
- Video: [Static Keyword in C Part 2/2](https://www.youtube.com/watch?v=gr90KDyLQdc) — HackerCS
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/syntax-static
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-6

## Question

Write the syntax used in C programming language for 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.

## Answer

`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;`

```c
#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:
```text
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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Static variable declaration?
   A. data_type static name
   B. static: type name
   C. stat type name
   D. static data_type name = value;
   Answer: D. Syntax. (Key point: Syntax)

2. Static keeps?
   A. Value between calls
   B. Nothing
   C. Address changes
   D. Only in main
   Answer: A. Lives whole run. (Key point: Persistence)

3. Initialised how often?
   A. Every call
   B. Once, default 0
   C. Never
   D. Twice
   Answer: B. Once. (Key point: Init)

4. Third call to counter prints?
   A. static = 1, auto = 3
   B. static = 3, auto = 3
   C. static = 3, auto = 1
   D. static = 1, auto = 1
   Answer: C. auto resets. (Key point: Output)

## Related in Computer Programming

- [Define programming language](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-programming-language) — Computer Programming, chapter 1, asked 3×
- [Syntax of while loop](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asyntax-while) — Computer Programming, chapter 5
- [Syntax of if...else](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asyntax-if-else) — Computer Programming, chapter 5
- [Syntax of struct](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asyntax-struct) — Computer Programming, chapter 8
- [Formatted I/O in FORTRAN with syntax](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-formatted-io-syntax) — Computer Programming, chapter 11
- [I/O functions used with data files](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-io-functions) — Computer Programming, chapter 10, asked 2×

## Related in other subjects

- [Comparators used in static relays](https://www.bctengineer.com/question.md?key=semester_6%2Fswitchgear_and_protection%3Acomparators-static-relay) — Switchgear and Protection, chapter 8, asked 2×
- [8086 assembly language operators](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3A8086-operators) — Microprocessor, chapter 3, asked 3×
- [Syntax of operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-syntax) — Object Oriented Programming, chapter 5, asked 4×
- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 4×
