> ## Documentation Index
> Fetch the complete documentation index at: https://learn.acadlink.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Variables

> Declaring, initializing, and classifying variables in C

# 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:

```c theme={null}
int number;
```

**Initialization** — declaration + assigning a value:

```c theme={null}
int number = 10;
```

**Syntax:**

```
data_type variable_name;
data_type variable_name = value;
```

***

## 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 with `printf()` 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`        |

```c theme={null}
printf("%d", 25);       // 25
printf("%.2f", 5.6789); // 5.68
printf("%.2lf", 3.14);  // 3.14
printf("%c", 'A');      // A
printf("%s", "Hello");  // Hello
```

***

## Example

```c theme={null}
#include <stdio.h>

int main() {
    int age = 20;
    float cgpa = 8.75;
    char grade = 'A';

    printf("Age  : %d\n", age);
    printf("CGPA : %.2f\n", cgpa);
    printf("Grade: %c\n", grade);

    return 0;
}
```
