Variables in C
A variable is a named container that occupies a memory location to store a value. That value can be read and changed during program execution.Declaration and Initialization
Declaration — reserves memory for the variable:Naming Conventions
| Style | Example | Used In |
|---|---|---|
| Pascal Case | UserName | Types, structs |
| Camel Case | userName | Variables, functions |
| Snake Case | user_name | C variables, Python |
| Kebab Case | user-name | Not valid in C |
Variable Classification by Data Type
| Data Type | Keyword | Size | Range |
|---|---|---|---|
| Character | char | 1 byte | −128 to 127 |
| Integer | int | 4 bytes | ±2.1 billion |
| Short Integer | short | 2 bytes | −32,768 to 32,767 |
| Long Integer | long | 4–8 bytes | Platform dependent |
| Float | float | 4 bytes | ±3.4 × 10³⁸ |
| Double | double | 8 bytes | ±1.7 × 10³⁰⁸ |
| Long Double | long double | 10–16 bytes | Even higher |
Variable Classification by Scope
| Type | Declared In | Scope | Lifetime |
|---|---|---|---|
| Local | A function/block | That block only | Until block ends |
| Global | Outside functions | Whole file | Whole program |
| Static | With static | Limited, keeps value | Until program ends |
| Extern | Another file | Global | Depends on origin |
| Register | With register | CPU register | Until block ends |
Format Specifiers
Used withprintf() and scanf() to read/print specific types:
| Specifier | Data Type | Example Output |
|---|---|---|
%d | int | 25 |
%f | float | 5.68 |
%lf | double | 3.14 |
%c | char | A |
%s | string | Hello |