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
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
#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
age = 21
height = 5.9
grade = A
a + b + c = 206. 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
1. Which of the following is a valid C variable name?
2. What is the default value of an uninitialized local (automatic) variable in C?
3. Which statement about C identifiers is TRUE?
4. int a, b = 5, c; — after this statement, what are the values of a and c?
5. What is the correct term for reserving memory and giving a variable an initial value in a single statement, e.g. int x = 10;?
Was this page helpful?
You May Also Like
Data Types in C
Master C data types — int, float, char, double, and modifiers — with sizes, ranges, format specifiers, and examples.
Constants in C
Understand C constants: literals, #define, const keyword, enumeration constants, and how to use each correctly.
Storage Classes in C
Learn C storage classes — auto, register, static, and extern — covering scope, lifetime, linkage, and exam pitfalls.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics