WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Intermediate · question 34 of 100

What are the different storage classes in C (e.g., auto, register, static, extern)?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

In C programming language, storage classes are used to specify the lifetime and visibility of variables. There are four storage classes in C: auto, register, static, and extern.

auto: The auto storage class is used to declare variables that have a local scope and are automatically allocated and deallocated when the function in which they are defined exits. If no storage class is specified for a variable, it is assumed to have auto storage class.

    void foo() {
        auto int x = 5;
        printf("%dn", x);  // prints 5
    }

register: The register storage class is used to declare variables that should be stored in a CPU register for faster access. The register storage class is only a suggestion to the compiler, and the compiler may ignore it if there are no available registers.

    void foo() {
        register int x = 5;
        printf("%dn", x);  // prints 5
    }

static: The static storage class is used to declare variables that have a local scope but are allocated statically and retain their values across multiple function calls.

    void foo() {
        static int x = 0;
        x++;
        printf("%dn", x);
    }
    
    int main() {
        foo();  // prints 1
        foo();  // prints 2
        foo();  // prints 3
        return 0;
    }

extern: The extern storage class is used to declare variables that are defined in another file or module. Variables declared with the extern storage class are not allocated storage space in the current file, but the compiler assumes that they exist in another file or module.

    // file1.c
    int x = 5;
    
    // file2.c
    extern int x;
    
    int main() {
        printf("%dn", x);  // prints 5
        return 0;
    }

It is important to note that the storage class of a variable affects its lifetime and scope. Variables with auto storage class have a lifetime that is limited to the block in which they are defined, whereas variables with static storage class have a lifetime that extends beyond the block in which they are defined. Variables with register storage class may or may not have a longer lifetime than variables with auto storage class, depending on whether they are actually stored in a CPU register. Variables with extern storage class have a global scope and can be accessed from any part of the program.

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