WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

C Β· Basic Β· question 4 of 100

What is the difference between a local and a global variable?

πŸ“• Buy this interview preparation book: 100 C questions & answers β€” PDF + EPUB for $5

In C programming language, variables can be declared either as local or global. Here are the main differences between local and global variables:

Scope: The scope of a variable refers to the part of the program where the variable is visible and can be accessed. Global variables have a global scope, which means they can be accessed from any part of the program, including functions. Local variables, on the other hand, have a local scope, which means they can only be accessed within the block of code where they are declared.

    #include <stdio.h>
    
    int globalVariable = 10; // Global variable
    
    void exampleFunction() {
        int localVariable = 20; // Local variable
        printf("Global variable: %dn", globalVariable);
        printf("Local variable: %dn", localVariable);
    }
    
    int main() {
        exampleFunction();
        printf("Global variable: %dn", globalVariable);
        // printf("Local variable: %dn", localVariable); // This will result in an error
        return 0;
    }

In this example, globalVariable is declared outside of any function, making it a global variable. localVariable is declared inside the exampleFunction() function, making it a local variable. The exampleFunction() function is able to access both the global and local variables, while the main() function can only access the global variable. Attempting to access the local variable from outside of the function will result in an error.

Lifetime: The lifetime of a variable refers to the period of time during which the variable exists in memory. Global variables have a lifetime that extends throughout the entire program, while local variables have a lifetime that is limited to the block of code where they are declared.

    #include <stdio.h>
    
    int globalVariable = 10; // Global variable
    
    void exampleFunction() {
        int localVariable = 20; // Local variable
        printf("Global variable: %dn", globalVariable);
        printf("Local variable: %dn", localVariable);
    }
    
    int main() {
        exampleFunction();
        printf("Global variable: %dn", globalVariable);
        // printf("Local variable: %dn", localVariable); // This will result in an error
        return 0;
    }

In this example, the global variable globalVariable exists in memory for the entire duration of the program, while the local variable localVariable exists in memory only for the duration of the exampleFunction() function.

Understanding the difference between local and global variables is important because it can affect the behavior of the program. Global variables should be used sparingly because they can be accessed from any part of the program, which can make it difficult to keep track of their values. Local variables are usually preferred because they are easier to manage and can help prevent bugs and unexpected behavior.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview β€” then scores it.
πŸ“ž Practice C β€” free 15 min
πŸ“• Buy this interview preparation book: 100 C questions & answers β€” PDF + EPUB for $5

All 100 C questions Β· All topics