US Trends

what is a variable in c

A variable in C is a named storage location in memory that can hold a value that may change while the program runs.

Quick Scoop: Core Idea

  • A variable is a name you give to a chunk of memory.
  • It has a type (like int, float, char) that decides:
    • How many bytes it uses in memory
    • What kind of values it can store
    • What operations you can legally perform on it
  • You must declare a variable before using it, so the compiler knows its type and name.

Example:

c

int age = 25;
float price = 99.99;
char grade = 'A';

Here, age, price, and grade are variables.

How Variables Look in Code

1. Declaration

You tell C: “I want a variable of this type with this name.”

c

int count;
float temperature;
char letter;

General form:

c

<data_type> <variable_name>;

You can also declare multiple of the same type:

c

int a, b, c;
float x, y;

2. Initialization

You can assign a value at the moment of declaration:

c

int count = 0;
float pi = 3.14;
char symbol = '#';

Or do it later:

c

int count;
count = 10;

What Is Really Happening (Intuition Story)

Imagine your computer’s memory as a huge set of numbered lockers.

  • When you declare int score;, C:
    • Finds an empty locker (or set of lockers) big enough for an int
    • Gives that locker an internal numeric address
    • Lets you use the name score instead of remembering the actual address

So using score in your code is like saying “the value in that locker labeled score.”

Basic Rules for Variable Names

Variable names (also called identifiers):

  • Must start with a letter (az or AZ) or underscore (_)
  • Can contain letters, digits, and underscores (_)
  • Are case-sensitive (total and Total are different)
  • Cannot be a C keyword (int, for, if, return, etc.)
  • Cannot contain spaces or symbols like -, @, #, !

Valid:

c

int total;
float _average;
char name1;
int value_of_sum;

Invalid:

c

int 1value;     // starts with digit ❌
int for;        // keyword ❌
int my-value;   // hyphen not allowed ❌
int first name; // space not allowed ❌

Types of Variables in C (by Scope & Lifetime)

You’ll commonly hear about these categories:

  • Local variables

    • Declared inside a function or block
    • Only usable within that function/block
    • Created when the function is called, destroyed when it returns

    c

    void func() { int x = 10; // local to func }

  • Global variables

    • Declared outside all functions
    • Accessible from all functions in the file (and sometimes other files with extern)

    c

    int globalCount = 0; // global

    int main() { globalCount++; }

  • Static variables

    • Use static keyword
    • For a local static variable: it keeps its value between function calls

    c

    void counter() { static int c = 0; c++; printf("%d\n", c); } // Each call prints 1, then 2, then 3, ...

  • External variables (extern)

    • Declared in one file, referenced in another using extern

    c

    // file1.c int globalCount = 0;

    // file2.c extern int globalCount;

You don’t need to master all of these at once; start with local and global.

Mini Example: Variables in Action

c

#include <stdio.h>

int main() {
    int a = 10;
    int b = 5;
    int sum = a + b;

    printf("Sum = %d\n", sum);
    return 0;
}

What happens:

  • a, b, and sum each get their own memory location
  • a holds 10, b holds 5, sum holds 15
  • printf reads the value stored in sum and prints it

Key Takeaways (TL;DR)

  • A variable in C is a named memory location that stores a value which can change while the program runs.
  • You must declare it with a type before use.
  • Variable names must follow rules: start with letter/underscore, no spaces, no keywords, case-sensitive.
  • There are different kinds of variables (local, global, static, etc.), mainly differing in where they can be used and how long they live.

If you want, next we can do:

  • A quick quiz-style set of practice questions, or
  • A deep dive into scope and lifetime with diagrams.