C Programs Tutorials | IT Developer
IT Developer

C Programming - C Variables



Share with a Friend

C Programming - C Variables

A variable in C is a named storage location in memory used to hold data. Variables play a fundamental role in programming by allowing data manipulation and storage for calculations, logic, and data processing.

Key Points about Variables

  1. Declaration: Variables must be declared before use.
  2. Naming Rules:
    • Can contain letters, digits, and underscores (_).
    • Must begin with a letter or underscore.
    • Cannot be a keyword.
    • Case-sensitive (age and Age are different).
  3. Data Type: Every variable must have a type (int, float, etc.).

Syntax of Variable Declaration

datatype variable_name;

datatype variable1, variable2, ... , variablen;

Examples

int age;           // Declares an integer variable

float salary;      // Declares a floating-point variable

char grade;        // Declares a character variable

int x = 10, y = 20; // Declares and initializes multiple variables

Variable Initialization

Variables can be initialized at the time of declaration.

Syntax

datatype variable_name = value;

Examples

int num = 5;

float pi = 3.14;

char letter = 'A';

Types of Variables in C

  1. Local Variables:
    • Declared inside a function or block.
    • Accessible only within the block where they are declared.

Example:

void display() {

    int num = 10;  // Local variable

    printf("%d\n", num);

}

  1. Global Variables:
    • Declared outside all functions.
    • Accessible by all functions in the program.

Example:

int count = 0;  // Global variable

void increment() {

    count++;

}

  1. Static Variables:
    • Retain their value between function calls.
    • Default value is 0.

Example:

void demo() {

    static int x = 0;  // Static variable

    x++;

    printf("%d\n", x);

}

  1. Automatic Variables:
    • Default for local variables.
    • Keyword: auto (optional).

Example:

void display() {

    auto int num = 5;  // Automatic variable

    printf("%d\n", num);

}

  1. External Variables:
    • Declared using extern and defined elsewhere.
    • Used to share variables across multiple files.

Example:

extern int globalVar;

Scope of Variables

  1. Block Scope:
    • Variable accessible only within the block {} where it is defined.
  2. Function Scope:
    • Variables declared inside a function are accessible only within the function.
  3. File Scope:
    • Global variables are accessible throughout the file.
  4. Program Scope:
    • extern variables accessible across multiple files.

Lifetime of Variables

  1. Automatic:
    • Exist only during the execution of the block where they are declared.
  2. Static:
    • Retain their value between function calls.
  3. Dynamic:
    • Allocated and deallocated explicitly using malloc/free.

Example: Variables in Action

#include <stdio.h>

 

// Global variable

int globalVar = 10;

void demo() {

    // Local variable

    int localVar = 20;

    // Static variable

    static int staticVar = 0;

    staticVar++;

    printf("Local: %d, Static: %d, Global: %d\n", localVar, staticVar, globalVar);

}

int main() {

    demo();

    demo();

    return 0;

}

Output

Local: 20, Static: 1, Global: 10

Local: 20, Static: 2, Global: 10

Summary

Type Scope Lifetime Keyword

Local

Block

Within block execution

None

Global

File

Entire program execution

None

Static

Block/File

Entire program execution

static

External

File/Program

Entire program execution

extern

Automatic

Block

Within block execution

auto