Learn how to create variables and store data in C
What is this topic?
This guide explains C Variables - Store and Manage Data in simple terms, what it does, and how to use it in real C programs.
Why We Need It
- It helps you write correct and reliable C code.
- It makes your programs easier to read and maintain.
- It is used in real projects and interviews.
- It reduces common beginner mistakes.
- It builds a strong foundation for advanced topics.
Use Cases
- Building practical C programs step by step.
- Solving real coding tasks with clean logic.
- Preparing for exams, interviews, and projects.
- Understanding and improving existing C code.
What is a Variable?
A variable is a named storage location that holds a value. Think of it like a labeled box where you can store information.
┌─────────────────────┐
│ Variable Box │
│ Name: age │
│ Value: 25 │
└─────────────────────┘
Creating Variables
To create a variable, you need:
- Data type (what kind of value)
- Variable name (what to call it)
- Value (what to store in it)
Syntax
dataType variableName = value;
Example
int age = 25;
| Part | Meaning |
|---|---|
int |
Data type (integer) |
age |
Variable name |
25 |
The value stored |
Declaring vs Initializing
Declaring (Creating a Variable)
int age; // Create variable, but don't set a value yet
Initializing (Giving it a Value)
int age = 25; // Create AND set value
Declaring Multiple Variables
int x, y, z; // All are integers
int a = 5, b = 10, c = 15; // All with values
Basic Variable Example
#include <stdio.h>
int main() {
// Create variables
int age = 25;
float height = 5.9;
char grade = 'A';
// Use variables
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
return 0;
}
Output:
Age: 25
Height: 5.9
Grade: A
Naming Rules for Variables
Rules You MUST Follow
-
Start with letter or underscore
int name; // ✓ Good int _name; // ✓ Good int 2name; // ✗ Bad - starts with number -
Only letters, numbers, and underscores
int myAge; // ✓ Good int my_age; // ✓ Good int my-age; // ✗ Bad - hyphen not allowed int my age; // ✗ Bad - space not allowed -
Case sensitive
int myAge; // Different from MyAge int MyAge; // And different from myAge -
Can’t use C keywords
int int = 5; // ✗ Bad - int is a keyword int while = 10; // ✗ Bad - while is a keyword
Best Practices for Naming
✓ Good Variable Names
int studentAge = 20; // Clear and descriptive
float totalPrice = 99.99;
char userInitial = 'J';
✗ Poor Variable Names
int x = 20; // Too vague
float a = 99.99; // Doesn't explain what it is
char z = 'J'; // Not descriptive
Changing Variable Values
Variables can be changed after creation:
#include <stdio.h>
int main() {
int age = 25;
printf("Age: %d\n", age); // Prints 25
age = 26; // Change the value
printf("Age: %d\n", age); // Prints 26
return 0;
}
Output:
Age: 25
Age: 26
Practical Examples
Example 1: Store Personal Information
#include <stdio.h>
int main() {
// Store person information
char name[] = "John";
int age = 30;
float weight = 75.5;
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Weight: %.1f kg\n", weight);
return 0;
}
Example 2: Store and Calculate
#include <stdio.h>
int main() {
int price = 50; // Price of item
int quantity = 3; // How many we buy
int total; // Total cost
total = price * quantity; // Calculate total
printf("Price per item: $%d\n", price);
printf("Quantity: %d\n", quantity);
printf("Total: $%d\n", total);
return 0;
}
Output:
Price per item: $50
Quantity: 3
Total: $150
Common Mistakes
Mistake 1: Using Variable Before Declaring
age = 25; // ✗ Error - age not declared
int age;
Fix:
int age; // ✓ Declare first
age = 25; // Then use
Mistake 2: Wrong Data Type
int name = "John"; // ✗ "John" is text, not integer
printf("%d\n", name);
Fix:
char name[] = "John"; // ✓ Use correct type for text
printf("%s\n", name);
Mistake 3: Forgetting Semicolon
int age = 25 // ✗ Missing semicolon
Fix:
int age = 25; // ✓ Semicolon added
Quick Reference
// Declare and initialize variables
int number = 42;
float price = 19.99;
char letter = 'A';
char name[] = "Alice";
// Change values
number = 50;
price = 29.99;
// Use in printf
printf("Number: %d\n", number);
printf("Price: %.2f\n", price);
printf("Letter: %c\n", letter);
printf("Name: %s\n", name);
Practice Exercise
Create variables.c:
#include <stdio.h>
int main() {
// Declare variables
char name[] = "Sarah";
int age = 22;
float gpa = 3.8;
// Print information
printf("=== Student Info ===\n");
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("GPA: %.1f\n", gpa);
// Change values
age = 23;
printf("\nNext Year - Age: %d\n", age);
return 0;
}
Navigation
| Previous | Next |
|---|---|
| ← C Comments | C Data Types → |
Have Questions?
Comment below and we’ll help you! Questions about variables? Ask in the comments!