C Variables – Complete Guide
1. What is a Variable?
A variable in C is a named memory location used to store data. A variable has:
- Data Type – defines what kind of data it can store.
- Name – the identifier.
- Value – the stored data.
2. Declaring and Initializing Variables
Declaration – Reserves memory:
1
| int age; // no value yet
|
Initialization – Assigns a value:
Declaration + Initialization:
1
2
3
4
| int age = 25; // integer
float gpa = 3.75; // floating-point
char grade = 'A'; // single character
char name[] = "Bro"; // string (array of characters)
|
3. C Data Types (Basic)
Data Type | Example | Typical Size* |
---|
int | 42 | 4 bytes |
float | 3.14 | 4 bytes |
double | 3.14159 | 8 bytes |
char | ‘A’ | 1 byte |
_Bool | 0 / 1 | 1 byte |
*Size may vary depending on system and compiler.
4. Variable Scope
Scope defines where a variable can be accessed.
- Local Variables – Declared inside functions; exist only within that function.
1
2
3
| void func() {
int x = 5; // local
}
|
- Global Variables – Declared outside all functions; accessible from anywhere.
- Block Scope – Declared inside
{}
; accessible only inside that block.
1
2
3
| if (1) {
int y = 20; // block scope
}
|
5. Storage Classes
Storage classes define lifetime and visibility.
Keyword | Meaning |
---|
auto | Default for local variables. |
static | Retains value between function calls. |
extern | Declared in another file. |
register | Suggests storing in CPU register (faster access). |
6. Constants
Constants store fixed values that cannot change.
1
2
| const float PI = 3.14; // constant variable
#define MAX 100 // preprocessor constant
|
Used with printf()
/ scanf()
to display or read variables.
Integer
Specifier | Meaning |
---|
%d / %i | Signed integer |
%u | Unsigned integer |
%o | Octal |
%x / %X | Hexadecimal |
Floating-Point
Specifier | Meaning |
---|
%f | Decimal notation |
%.nf | Fixed decimal places |
%e / %E | Scientific notation |
%g / %G | Shortest representation |
Character & String
Specifier | Meaning |
---|
%c | Single character |
%s | String |
Pointer
Specifier | Meaning |
---|
%p | Memory address |
Special
Specifier | Meaning |
---|
%% | Prints % |
8. Example Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #include <stdio.h>
int globalVar = 100; // global variable
int main() {
int age = 25; // integer
float gpa = 3.75; // float
char grade = 'A'; // char
char name[] = "Bro"; // string
const float PI = 3.14; // constant
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("GPA: %.2f\n", gpa);
printf("Grade: %c\n", grade);
printf("PI: %.2f\n", PI);
printf("Global Var: %d\n", globalVar);
printf("Memory address of age: %p\n", (void*)&age);
return 0;
}
|