×

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially declared.

This can cause various issues and there must be a way to restrict this access. If the programmer wants to restrict this access, then there is a simple solution to that making the declared function as static.

A function can be made static by simply putting a keyword static in front of the intended function.

Function in C

A function is a self-contained block of statements that performs a specific task. A function wraps a number of statements which are repeated in a program into a single unit or block. This wrapping of statements(unit) is identified by a name known as a function name.

Note 1: Every C program no matter how easy and low in the number of lines of code will always consist of at least one function, which is the main() function.

Note 2: In C programming language, functions are global by default.

Declaration :

returnType  functionName( parameters)
 {
   		function body
 }

Example:       

#include<stdio.h>
int multiplication();  // function declaration


int main()
{   


   int output;   //local variable definition   
    
   output = multiplication();


    printf("The product of the two numbers is: %d\n",output);
    return 0;
}


int multiplication()
{
    int num1 = 5;
    int num2 = 15;


    return num1 * num2;
}

Static Function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially declared.

A function can be made static by adding ‘static’ keyword before it.

Example 1: non static function

int sum(int a, int b)
{
printf(“This a non-static function, it is global by default”);
}

Example 2: static function

static int fun1(void)
{
printf(“This is a static function”);
}

In Example 1, the function sum() is not static. This means that if we want to access sum() outside of the file where sum() was declared, we can access it with ease.

But, In Example 2, the function fun1() is declared static. Unlike sum() which is a global function, access to fun1() is restricted to the file where fun1() is declared.

The other reason to make a function static is the reusability feature. Making functions static allows reuse of the same function name in other files. Also, By declaring a function as static would also provide the security.

Let’s understand the importance of static function with the help of an example.

Program 1

Suppose, inside your C project you have two files, file_1.c and main.c.

The contents of both files are as follows:

1) file_1.c

static int sum(int a, int b)
{
int result;
result = a+b;
return result;
}

2) main.c

int sum(int, int);
int main()
{
int addition = sum(5, 7);
printf(“%d”, addition);
return 0;
}

Code Explanation:

If the above code is compiled, then an error will be reflected on our console. This error occurs due to the declaration of variable sum as static. By declaring sum() as static, the function is only visible in file_1.c and hence its access is limited within this file.

Program 2: A program in C demonstrating static functions

#include<stdio.h>
static int mul(int a, int b)  // declaring the function as static
{
int result;  // declaring the local variables


result = a*b;
printf("We are inside the static function - mul() \n ");
return result;
}


int mul(int, int);
int main()
{
int product = mul(3, 8);


printf("The output of the intended calculation is %d", product);
return 0;
}

Output of the program:

Static function in C

Code explanation:

In the above program, the function mul() is declared as a static function that performs multiplication operation on two numbers and prints a statement “We are inside the static function - mul() “ .

When a function call is performed by the main() function, the main() function calls the mul() function and we get the desired output.

The program works correctly as the static function mul() is called from its own file. i.e., both mul() and main() are declared inside one file and hence their access lies within this file. That’s why we are able to fetch the desired results from above code even though the mul() function is declared as static.


Related Topics

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Kruskal algorithm in C

Given a weighted graph, Kruskal's algorithm generates a spanning tree with the lowest possible weights. Start by creating an edge list for the given graph, including the weights. Sort the...

3 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.