C Variables
- What is a Variable?
- A variable is a container (memory location) used to store data values.
- Each variable has:
- A type → defines the kind of data stored
- A name → used to identify it
- A value → the actual data stored in it
👉 Think of a variable as a box that holds a value.
- Types of Variables in C
|
Data Type |
Meaning |
Example |
|
int |
Stores integers (whole numbers) without decimals |
int age = 23; |
|
float |
Stores floating-point numbers (numbers with decimals) |
float price = 19.99; |
|
char |
Stores single characters (enclosed in single quotes) |
char grade = ‘A’; |
- Declaring (Creating) Variables
To create a variable, we must:
- Specify the type
- Give it a name
- (Optionally) Assign a value
Syntax:
type variableName = value;
Example:
int myNum = 15;
✅ Here:
- int → data type
- myNum → variable name
- 15 → value
- Declaring Without Assigning
You can declare a variable first and assign a value later.
Example:
int myNum; // Declaration
myNum = 15; // Assignment 5. Outputting Variables
Normally, you can print text with:
printf(“Hello World!”);
But to print variable values, C requires format specifiers (like %d, %f, %c).
If you try this:
int myNum = 15;
printf(myNum); // ❌ Nothing happens
It won’t work because C doesn’t automatically know what type of data you’re printing.
You’ll learn how to print variables in the next chapter using format specifiers.
🧩 Quick Summary
|
Concept |
Description |
|
Variable |
A container that stores data |
|
Syntax |
type variableName = value; |
|
Example |
int age = 25; |
|
int |
Whole numbers |
|
float |
Decimal numbers |
|
char |
Single character |
|
Output |
Use printf() + format specifiers |