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
ย orยmarks
). - 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, butย1stStudent
ย is invalid. - Use only letters, numbers, or underscores.ย Spaces and special symbols (likeย
@
,ย#
) are not allowed. For example,ยtotal_score
ย is valid, butยtotal score
ย is not. - C is case-sensitive.ย This meansย
Age
,ยAGE
, andยage
ย are three different variables. - Do not use C keywords.ย Words likeย
int
,ยfloat
, andยreturn
ย 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
ย andยnum2
ย store user input.sum
ย calculates and displays the result.