100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C

Variables in C

Learn C variables: declaration, initialization, naming rules, scope, and exam-ready examples with compilable code.

Basics & Data TypesBeginner8 min readJul 7, 2026
Analogies

1. Introduction

A variable in C is a named location in memory that a program can use to store a value that may change during execution. Every variable in C has a name (identifier), a data type that determines what kind of value it can hold, and a value that can be read or modified at run time.

🏏

Cricket analogy: A variable is like a named scoreboard slot such as 'strikerRuns' that holds a number which changes ball by ball, with a type (integer runs) determining what kind of value that slot can hold.

Variables are fundamental to any C program because they let you store user input, intermediate calculation results, and program state. Understanding how variables are declared, initialized, and scoped is one of the most frequently tested topics in C programming exams.

🏏

Cricket analogy: Variables are essential because they store the current run rate entered live, intermediate calculations like required run rate, and match state like wickets fallen — and mismanaging their scope is a classic scorer's exam mistake.

2. Syntax

c
dataType variableName;
dataType variableName = value;
dataType var1, var2, var3;
dataType var1 = value1, var2 = value2;

3. Explanation

A variable declaration tells the compiler the name and type of the variable, which determines how much memory to allocate and how the stored bits should be interpreted. A variable definition (declaration with allocation) can optionally include initialization, where an initial value is assigned at the point of declaration using the assignment operator '='.

🏏

Cricket analogy: A declaration is like naming a new scoreboard slot 'extras' as an integer, telling the scorer how it should be tallied; a definition also allocates a real spot on the board and can initialize it to 0 right away using '='.

C variable names (identifiers) must begin with a letter or underscore, followed by letters, digits, or underscores; they cannot begin with a digit, cannot contain spaces or special symbols like '@' or '-', and cannot be a C reserved keyword such as 'int', 'return', or 'for'. C is case-sensitive, so 'count' and 'Count' are different variables. Uninitialized local variables contain indeterminate (garbage) values, while uninitialized global and static variables are automatically zero-initialized.

🏏

Cricket analogy: Naming a variable 'runs1' is fine but '1runs' isn't allowed, just as C identifiers can't start with a digit; 'Score' and 'score' are treated as different scoreboard entries since C is case-sensitive, and an uninitialized local tally holds garbage while a global wicket counter defaults to zero.

Tip: Always initialize local variables before use. Reading an uninitialized local variable is undefined behavior in C and a common source of exam trick questions.

4. Example

c
#include <stdio.h>

int main(void) {
    int age = 21;              /* declaration + initialization */
    float height;               /* declaration only */
    char grade = 'A';
    int a, b, c = 10;           /* multiple declaration */

    height = 5.9f;               /* assignment after declaration */
    a = 3;
    b = 7;

    printf("age = %d\n", age);
    printf("height = %.1f\n", height);
    printf("grade = %c\n", grade);
    printf("a + b + c = %d\n", a + b + c);

    return 0;
}

5. Output

text
age = 21
height = 5.9
grade = A
a + b + c = 20

6. Key Takeaways

  • A variable must be declared with a data type before use in C.
  • Identifiers start with a letter or underscore and cannot be a reserved keyword.
  • C is case-sensitive, so 'sum' and 'Sum' are distinct variables.
  • Local (automatic) variables hold garbage values until explicitly initialized.
  • Global and static variables are zero-initialized by default.
  • Multiple variables of the same type can be declared in one statement, comma-separated.

Practice what you learned

Was this page helpful?

Topics covered

#CProgrammingStudyNotes#Programming#VariablesInC#Variables#Syntax#Explanation#Example#StudyNotes#SkillVeris#ExamPrep