×

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both as a static variable and a static function.

Static variables have the advantage of preserving their value even if they are out of scope. A regular variable's scope is confined to how it is defined, whereas a static variable's scope is unlimited. Static variables preserve their previous value in their earlier scope and are not initialized again in the new scope.

Syntax:

static dataTypevariableName = variableValue;

In English, the word static means "pertaining to or characterised by a permanent or immobile situation." Computer language, on the other hand, is used to represent things that do not change over time.A variable that is stored using static is assigned address during build time. It will therefore be safe to use a pointer to such a variable.

  • During the building process, a variable that is saved using static is given an address. As a result, using a reference to a very variable will be safe.

E.g., While the application is compiling and executing, a static int variable remains in memory. When a function call where the variable is created ends, the regular variable is destroyed.

#include <stdio.h>
	int fun()
	{
		static int count = 0;
		count++;
		return count;
	}
	int main()
	{
		printf (“%d \n”, fun());
		printf (“ %d \n”, fun());
		return 0;
	}

Output:

1 2
  • Static variables are assigned memory in the data segment, not the stack segment, and a static function's lifespan spans the whole programme.
  • Inside a structure, static variables should not be defined. The reason for this is that the C compiler expects all of the structure components to be in the same place. That is, structure members' memory allocation should be contiguous. It will be allowed to specify structure within the function, as well as dynamically or globally allocate memory.All structure members should reside in the same memory because the value for the structure element is fetched by counting the offset of the element form the beginning address of a structure. Differentiating one member from the data segment negates the purpose of a centralized and decentralized, since a static variable can be applied to the whole structure.
  • Static variables, such as global variables, are initialized as 0.

E.g., the value of X can be printed as 0. In comparison, the value of Y is something garbage.

#include <stdio.h>
	int main() 
	{
		static int x;
		int y;
		printf(“ %d \n”, x, y);
	}
  • In the C programming language, static variables can be initialized using constant literals.
  • A static local variable is a local variable that has been declared using the static keyword. When a function updates a static local variable, the changed value is available to all subsequent functions.Static functions and static global variables are possible in the C programming language.
  • A function's static variable retains its value between invocations.
  • A static method is a member function of a class that is defined using the static keyword. It may be accessible by all instances of a class; however, it cannot be limited to a single instance.
  • A static global variable or a function is seen only in its file where it is declared at.

E.g.:

#include <stdio.h>
#include <conio.h>
void book()
{
int x = 10;
static int y = 10;


x += 5;
y += 5;


printf (“x = %d, y = %d \n”, x, y);
}
int main()
{
int i;
for (i = 0; i<10; ++i)
	book();
}

Output:

x = 15, y = 15
x = 15, y = 20
x = 15, y = 25
x = 15, y = 30
x = 15, y = 35
x = 15, y = 40
x = 15, y = 45
x = 15, y = 50

If a function has to retain some state among invocations and you don't want to employ global variables, static help is helpful. This should be used with caution, as it may make your code thread-safe while also making it more difficult to understand.

Static is also often used as a kind of access control. Developing various features in the C programming language exposes a few public functions to users; the rest of the functions should be made static so that the user cannot access them, which is known as encapsulation.

The use of static keywords has a variety of applications. Limiting the scope of variables declared within a function or module is the most effective use.

Static variables and static functions can be concealed from the outside world by using static keywords, which behaves similarly to the usage of private and public in more object-oriented languages.


Related Topics

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.