C Programming

Static Functions in the C Computer Language

“static” is a reserved word in the computer language, C. It is a storage class specifier that can be used with functions. A C program can have more than one file. Only one of the files should have the C main() function. In this article only two files are considered: the file with the C main() function which is called mainFile.c and the other file without the main function which is called otherFile.c

Global Function

A global function is a function defined in a C file before the main() function. Even the main() function is a global function but the main() function is not the focus in this article. In the language C, programmer-defined functions are usually global functions and simply referred to as functions.

A global function can be called in any scope in its file. In order for a global function to be seen in a different file, its prototype has to be declared in that different file (see below.) To prevent a function defined in one file from being seen in a different file, precede the definition with the reserved word, static. With that, the static function would be a global function only in its own file and will not be seen in another file.

So, a global function defined in otherFile.c can be seen anywhere in otherFile.c. For it to be seen in mainFile.c, its prototype has to be declared in mainFile.c (see below.) Its definition in otherFile.c should not be preceded by “static”. To prevent it from being seen in mainFile.c, its definition in otherFile.c has to be made staticremoved a comma by preceding it with the word, static.

This article illustrates static function in the computer language, C beginning with the meaning of function prototype and its use in a C program of two or more files.

Compilation of the files, mainFile.c and otherFile.c, can be done with the following Bash terminal command:

gcc mainFile.c otherFile.c -o temp.exe

temp.exe is the name of the resulting single executing file.

Article Content

– Introduction – see above

– C Function Prototype

– Global Function

– Static Function Proper

– Conclusion

C Function Prototype

Consider the global function definition:

    char* fn1(char* stri) {
        return stri;
    }

The prototype for this function is:

char* fn1(char* stri);

It is the function signature, ending in semicolon.

Now, let the content of the otherFile.c be:

char* fn1(char* stri) {

return stri;

}

Assume that the file, otherFile.c has only the definition of the global function, fn1(). Now let the content of the file, mainFile.c, be:

    #include <stdio.h>

    char* fn1(char* stri);

    int main()
    {
        char* str = fn1("seen");
printf("%s\n", str);

        return 0;
    }

It begins with the inclusion of the <stdio.h> header (library). This is followed by the prototype declaration of the function, fn() of the other file.

This main file does not have the definition of any global function other than the default main() function. In the main function, the first statement calls function fn1() defined in the other file. This call would not be effective if the prototype of fn1() was not declared in this file, mainFile.c.

If the reader has coded the above two files, then he can compile the program into one executable file with the following command:

gcc mainFile.c otherFile.c -o temp.exe

pressing the Enter key at the end of the line. The output should be, “seen”.

Global Function

The mainFile.c file can be modified as follows:

    #include <stdio.h>

    char* fn1(char* stri);

    char* fn(char* st) {
        return st;
    }

    int main()
    {
        char* str1 = fn1("seen");
printf("%s\n", str1);
        char* str2 = fn("Global seen in its file.");
printf("%s\n", str2);
        return 0;
    }

Now, there are two global functions in the file, mainFile.c. The names of the functions are fn() and main(). dfn() is a global function. It has been seen in the main() function local scope, by a call. In the language C, a global function in the global scope, such as fn(), is simply referred to as a function. Also, the global function, fn1() in the file, otherFile.c is simply referred to, as a function.

If the two files are re-compiled into temp.exe, then the outout will be:

seen

Global seen in its file.

Note: the function definition for fn() can also be made to be seen in the other file, otherFile.c. To achieve that have its prototype declaration in otherFile.c, as follows:

char* fn(char* st);

ending with a semicolon. This activity, is left as an exercise for the reader.

Static Function Proper

From the above discussion, a function can be seen in any scope (anywhere) in its file. It can also be seen in a different file, for the same program, if its prototype is declared there. In order to prevent the definition of the file from being seen in a different file, make the definition static by preceding it with the reserved word, static. Even if its prototype is declared in the different file, it will still not be seen in the different file. The program files will not even compile. To illustrate this, let the content of the file, otherFile.c be:

static char* fn1(char* stri) {

return stri;

}

This is the same content in the file, otherFile.c, as before, but with the preceding reserved word, static. The content of the mainFile.c file, remains the same. If an attempt is made to compile the program files with the command,

gcc mainFile.c otherFile.c -o temp.exe

the compiler would issue an error message, meaning the program did not compile. Even if the prototype declaration in the different file, is also preceded by static, the program files will still not compile.

Conclusion

A function definition in the C language, is a global function. This function can be called (seen) in any scope in its file. In order for this function to be seen in a different file, such as the main file, its prototype declaration has to be typed in that different file. To prevent this function from being seen in the different file make its definition static by preceding it with the reserved word, static.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.