Variables are the foundation of programming in C. They allow you to store and manipulate data. This makes your programs dynamic and interactive. When you write a program, you need variables to hold numbers, text, or other values that the program uses.
What is Variable in C
A variable is a name given to a memory location where data is stored. Every variable has three main parts:
- Name: A unique identifier for the variable (like
age
ormarks
). - Type: The kind of data it can store (like numbers, decimals, or letters).
- Value: The actual data stored in the variable (like
20
or'A'
).
Example:
int age = 15;
Here, age
is the variable name, int
is the data type, and 15
is the value.
Types of Variables in C
Variables in C can be of different types based on where they are declared and how long they exist.
1. Local Variables
- Declared inside a function.
- It can only be used in that function.
void myFunction() {
int x = 10; // local variable
}
2. Global Variables
- Declared outside all functions.
- It can be used anywhere in the program.
int score = 100; // global variable
void checkScore() {
printf("%d", score); // works
}
3. Static Variables
- Retains its value between function calls.
void counter() {
static int count = 0;
count++;
printf("%d", count);
}
// Output: 1, 2, 3... (remembers previous value)
4. Automatic Variables
- Default type (created when a function starts, destroyed when it ends).
void demo() {
auto int x = 5; // same as 'int x = 5;'
}
5. External Variables
- Used to share variables between multiple C files.
extern int globalVar; // declared in another file
Data Types in C
Variables store different types of data. C has four main data types:
Data Type | Example | Usage |
---|---|---|
int | int age = 15; | Stores whole numbers |
float | float pi = 3.14; | Stores decimal numbers |
char | char grade = 'A'; | Stores single characters |
double | double bigNum = 1234.5678; | Stores large decimal numbers |
Key Points:
int
= Whole numbers (no decimals).float
= Decimal numbers (6-7 digit precision).double
= Bigger decimal numbers (15-16 digit precision).
Rules for Naming Variables in C
When you create a variable, you must follow certain rules:
- Start with a letter or underscore (
_
). You cannot start a variable name with a number. For example,score
and_count
are valid, but1stStudent
is invalid. - Use only letters, numbers, or underscores. Spaces and special symbols (like
@
,#
) are not allowed. For example,total_score
is valid, buttotal score
is not. - C is case-sensitive. This means
Age
,AGE
, andage
are three different variables. - Do not use C keywords. Words like
int
,float
, andreturn
cannot be variable names because they have special meanings in C.
A good practice is to use meaningful names. For example, use studentAge
instead of just x
so that your code is easier to understand.
How to Declare and Initialize Variables
Declaring a variable means telling the compiler its name and type. Initializing means giving it a value.
Declaration
Creating a variable by specifying its type and name.
int age; // Declared but not initialized
At this point, age
has no value, and using it may give unexpected results.
Initialization
Assigning a value to the variable for the first time.
age = 20; // Now it has a value
Declaration + Initialization (Best Way)
You can also declare and initialize in one step:
int marks = 95; // Declared and initialized
Constants in C
A constant is a variable whose value cannot be changed.
Example:
const float PI = 3.14159;
If you try to change it later, the compiler will give an error.
Constants are useful for values that should never change, like mathematical constants.
Common Mistakes with Variables
Beginners often make these mistakes:
1. Using Uninitialized Variables
int x;
printf("%d", x); // Prints garbage value
Always assign a value before using a variable.
2. Wrong Data Type
float a = 10; // Works, but 'int' is better for whole numbers
Use int
for whole numbers and float
for decimals.
3. Misspelling Variable Names
int age = 20;
printf("%d", Age); // Error (case-sensitive)
Always check spelling and case.
Practical Example of a C Program
Let’s write a simple C program using variables:
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("Sum = %d", sum);
return 0;
}
Explanation:
num1
andnum2
store user input.sum
calculates and displays the result.