Skip to main content
Engineering LibreTexts

5.2: Scope

  • Page ID
    35822
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Scope has to do with where variables are “seen”. We have already mentioned the idea of global and local in previous work but it is time to delve a little deeper. Generally, variables only exist within the block they are declared. While it is legal to declare variables inside of a conditional or loop block, we normally declare variables at the very beginning of a function. Consequently, these variables are known within the function. That is, their scope of reference is within the function. Nothing outside of the function knows anything about them. Thus, we say that they are local, or perhaps localized, to the function. For example, consider the two function fragments below:

    void func1( void )
    {
        int x;
        int y;
        //...some code here...
    }
    
    void func2( void )
    {
        int y;
        int z;
        //...some other code here...
    }
    

    There is no direct way to access the z variable of func2() from func1(). Likewise, there is no direct way to access the x variable of func1() from func2(). More interestingly, the y variables of func1() and func2() are entirely different! They do not refer to the same variable. This sometimes can be confusing for new programmers but it is essential for large programs. Imagine that you were working with a team of people on a very large program, perhaps tens of thousands of lines long. If the idea of local scope did not exist, you’d have to make sure that every single variable in the program had a unique name! This would create a nightmare of confusion. By using local scope, you’re saying: “I only need this variable for a job within this function. As it only needs to exist within this function, its name is only meaningful within this function.”

    If some form of “universally known” data item is needed, we can resort to the global. Globals act pretty much like statics and are usually stored the same way. If you have a program that consists of a single file, you can declare your globals by listing them at the beginning of the program before (and outside of) any functions. In this way they will be read by the compiler first and will therefore be “known” to all functions that follow. Do not get in the habit of declaring all variables as global. This is considered a bad and inefficient coding method. Get into the habit of using locals as the norm and resort to globals only when called for.

    If you have a multiple file project, how do you get the functions in the second file to recognize the globals declared in the first file? In this case, you’ll create a header file and use the #include directive. For example, suppose your project consists of two C source files named foo.c and bar.c.

    In foo.c you declare the following global:

    int m;

    In order for the functions in bar.c to “see” m, you’ll create a header file, perhaps called foo.h. foo.h will contain the following:

    extern int m;

    Meaning that an integer named m has been declared externally (i.e., in another file). At the top of bar.c you’ll add the line:

    #include <foo.h>

    So, when bar.c is compiled, the compiler will first open up foo.h. It will see that the variable m has been declared elsewhere and puts it in a “known variables list”. It then continues to compile the remainder of you code. When it comes across m in some function, the compiler “understands” that this is a variable that was declared in another file. No problem!

    So, you can now see that header files are largely composed of definitions and declarations from other places, namely external data and function prototypes.


    This page titled 5.2: Scope is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by James M. Fiore via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?