C

C Programming

Master the fundamentals of C programming with comprehensive tutorials, examples, and hands-on exercises

Storage Classes in C

Storage classes define the scope, visibility, and lifetime of variables and functions in C. They determine where variables are stored in memory and how long they persist.

auto Storage Class

Automatic Variables (Default)
#include <stdio.h>

void function() {
    auto int x = 10;  // auto keyword (optional)
    int y = 20;       // Same as auto int y = 20;
    
    printf("Inside function: x = %d, y = %d\n", x, y);
    x++;
    y++;
    printf("After increment: x = %d, y = %d\n", x, y);
}

int main() {
    function();
    function();  // Variables are reinitialized each call
    
    // auto variables are destroyed when function ends
    // printf("%d", x);  // Error: x not accessible here
    
    return 0;
}

static Storage Class

Static Variables
#include <stdio.h>

void counter() {
    static int count = 0;  // Initialized only once
    count++;
    printf("Function called %d times\n", count);
}

static int globalStatic = 100;  // File scope only

void showGlobal() {
    printf("Global static variable: %d\n", globalStatic);
    globalStatic++;
}

int main() {
    printf("Demonstrating static local variables:\n");
    counter();  // Output: 1
    counter();  // Output: 2
    counter();  // Output: 3
    
    printf("\nDemonstrating static global variables:\n");
    showGlobal();  // Output: 100
    showGlobal();  // Output: 101
    
    return 0;
}

extern Storage Class

External Variables
// file1.c
#include <stdio.h>

int globalVar = 50;  // Global variable definition

void printFromFile1() {
    printf("From file1: globalVar = %d\n", globalVar);
}

// file2.c (separate file)
#include <stdio.h>

extern int globalVar;  // Declaration (defined in file1.c)

void printFromFile2() {
    printf("From file2: globalVar = %d\n", globalVar);
    globalVar += 10;
}

// main.c
#include <stdio.h>

extern int globalVar;  // Declaration
extern void printFromFile1();  // Function declaration
extern void printFromFile2();  // Function declaration

int main() {
    printf("Initial value: %d\n", globalVar);
    
    printFromFile1();
    printFromFile2();
    
    printf("Final value: %d\n", globalVar);
    
    return 0;
}

register Storage Class

Register Variables (Hint to Compiler)
#include <stdio.h>

int main() {
    register int i;  // Suggest storing in CPU register
    register int sum = 0;
    
    // Frequently used variables in loops
    for (i = 1; i <= 1000; i++) {
        sum += i;
    }
    
    printf("Sum of 1 to 1000: %d\n", sum);
    
    // Note: Cannot take address of register variables
    // printf("Address: %p", &i);  // Error!
    
    return 0;
}

Storage Class Comparison

Complete Example
#include <stdio.h>

// Global variables
int globalVar = 100;           // External linkage
static int fileStatic = 200;   // Internal linkage

void demonstrateStorage() {
    auto int autoVar = 10;        // Automatic storage
    static int staticVar = 20;    // Static storage
    register int regVar = 30;     // Register storage (hint)
    
    printf("\nInside function:\n");
    printf("Auto variable: %d\n", autoVar);
    printf("Static variable: %d\n", staticVar);
    printf("Register variable: %d\n", regVar);
    printf("Global variable: %d\n", globalVar);
    printf("File static variable: %d\n", fileStatic);
    
    // Modify variables
    autoVar++;
    staticVar++;
    regVar++;
    globalVar++;
    fileStatic++;
    
    printf("\nAfter increment:\n");
    printf("Auto variable: %d\n", autoVar);
    printf("Static variable: %d\n", staticVar);
    printf("Register variable: %d\n", regVar);
    printf("Global variable: %d\n", globalVar);
    printf("File static variable: %d\n", fileStatic);
}

int main() {
    printf("Storage Class Demonstration\n");
    printf("===========================\n");
    
    printf("First function call:");
    demonstrateStorage();
    
    printf("\nSecond function call:");
    demonstrateStorage();
    
    printf("\nThird function call:");
    demonstrateStorage();
    
    return 0;
}
Storage Class Summary:
  • auto: Default for local variables, automatic lifetime
  • static: Preserves value between function calls, file scope for globals
  • extern: Declares variables defined in other files
  • register: Suggests CPU register storage (compiler hint)

Frequently Asked Questions

C is a powerful, general-purpose programming language that's been around since the 1970s. It's the foundation for many modern languages and is essential for system programming, embedded systems, and understanding how computers work at a low level. Learning C gives you a solid foundation in programming fundamentals.

No prior programming experience is required! Our C programming tutorial is designed for complete beginners. We start with the basics and gradually progress to more advanced topics. However, having some basic computer literacy and logical thinking skills will be helpful.

You need a C compiler to run C programs. For beginners, we recommend using our online C compiler which requires no installation. For local development, you can use GCC (GNU Compiler Collection) which is available on Windows (via MinGW), macOS (via Xcode), and Linux. IDEs like Code::Blocks, Dev-C++, or Visual Studio Code are also helpful.

The time to learn C depends on your dedication and prior experience. With consistent practice (1-2 hours daily), you can grasp the basics in 2-4 weeks and become proficient in 2-3 months. Mastering advanced concepts like pointers, memory management, and data structures may take 6-12 months of regular practice.

C programming skills open doors to various career paths including system programming, embedded systems development, operating system development, device driver programming, game development, and firmware development. Many companies in automotive, aerospace, telecommunications, and IoT sectors actively seek C programmers.

Absolutely! C remains highly relevant and is consistently ranked among the top programming languages. It's essential for system programming, embedded systems, IoT devices, and performance-critical applications. Many modern languages are built on C, and understanding C helps you become a better programmer overall.