Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is File Scope in C?

Umme Ammara

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In the C language, the variable's scope refers to the part of the program where we reference it. The two types of variables in C are:

1. Local variables are defined within a function body. They are not accessible outside the function.

2. Global variables are defined outside the function body. They are accessible to the entire program.

Types of Variable in C

Static specifier

The static specifier is one of the four standard storage classes in C.

A global static variable is declared before the function declarations in a C program using the static keyword, as shown below:

static int a = 10;

The four standard storage classes in C are static, register, auto, and extern.

File scope

The global static variable is accessible by all the functions in the same source file as the variable. This variable has a File Scope, i.e., we can access the variable anywhere in the same C file. Therefore, the variables with File Scope do not conflict with other variables because they are private to the source file.

Example

The code below shows the static global variable a. Since a has a File Scope, it is accessible both within and outside the function. It is referenced in lines 7 and 13, as shown below:

#include <stdio.h>
// A Static Global Variable
static int a = 20;
int func() {
a = a + 20;
printf ("The value of a is: %d\n", a);
}
int main() {
func();
a = a + 5;
printf ("The value of a is: %d\n", a);
return 0;
}

RELATED TAGS

c

CONTRIBUTOR

Umme Ammara
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring