# Data Types, Variables, Operators and printf/scanf in C

Source: https://skills101.in/learn/c-programming/c-data-types-and-operators — Skills101 free tutorial (C Programming). Updated 2026-11-09.

In C, every variable has a **type** that decides how many bytes it uses and what values it can hold.

![C data types on a typical 64-bit compiler: char 1 byte (%c), int 4 (%d), float 4 (%f), double 8 (%lf), long long 8 (%lld), pointer 8 (%p)](https://skills101.in/learn-img/c-programming/type-sizes.svg)

## 1. Declaring variables

```c
#include <stdio.h>

int main(void) {
    char grade = 'A';
    int age = 21;
    float price = 99.5f;
    double pi = 3.14159265;
    printf("%c %d %.1f %.4f\n", grade, age, price, pi);
    printf("char=%zu int=%zu double=%zu\n", sizeof(char), sizeof(int), sizeof(double));
    return 0;
}
```

```text
A 21 99.5 3.1416
char=1 int=4 double=8
```

- `sizeof` gives the size in bytes (`%zu` prints it).
- **Uninitialised local variables contain garbage** — always give a starting value.

### Format specifiers

| Specifier | Type |
|---|---|
| `%d` / `%i` | int |
| `%u` | unsigned int |
| `%ld` / `%lld` | long / long long |
| `%f` | float or double (printf) |
| `%lf` | double (scanf) |
| `%c` | char |
| `%s` | string |
| `%p` | pointer (address) |
| `%x` | hexadecimal |

`%.2f` = 2 decimal places, `%5d` = width 5 (right-aligned), `%-5d` = left-aligned.

## 2. Reading input with scanf

```c
#include <stdio.h>

int main(void) {
    // Input: 12 30
    int a, b;
    scanf("%d %d", &a, &b);
    printf("sum = %d\n", a + b);
    return 0;
}
```

```text
sum = 42
```

> Note the **`&`** before each variable: scanf needs the **address** where it should store the value. Forgetting `&` is one of the most common C bugs (strings are the exception — covered later).

## 3. Arithmetic and integer division

```c
#include <stdio.h>

int main(void) {
    printf("%d %d %d\n", 17 / 5, 17 % 5, -17 / 5);
    printf("%.2f\n", 17 / 5.0);
    printf("%.2f\n", (float)17 / 5);
    printf("%d\n", 7 / 2 * 2);
    return 0;
}
```

```text
3 2 -3
3.40
3.40
6
```

- `int / int` **cuts** the decimal part (towards zero: −17/5 = −3).
- Make one side a float (`5.0` or a cast) to get decimals.
- `7 / 2 * 2` = `3 * 2` = 6, not 7.

## 4. Increment and decrement

```c
#include <stdio.h>

int main(void) {
    int i = 5;
    int a = i++;   /* use 5, then i becomes 6 */
    int b = ++i;   /* i becomes 7, then use 7 */
    printf("%d %d %d\n", a, b, i);
    return 0;
}
```

```text
5 7 7
```

> Never write `i = i++` or use `i++` twice in one expression (`i++ + i++`). That's **undefined behaviour** in C — different compilers give different answers.

## 5. Relational, logical and other operators

- Relational: `==  !=  >  <  >=  <=` → result is **1 (true) or 0 (false)**.
- Logical: `&&`, `||`, `!` — with **short-circuit**: in `a && b`, if `a` is false, `b` is never evaluated.
- Assignment: `+=  -=  *=  /=  %=`
- Ternary: `cond ? x : y`
- Bitwise: `&  |  ^  ~  <<  >>` (`x << 1` doubles x).

```c
#include <stdio.h>

int main(void) {
    int x = 5;
    printf("%d %d %d\n", x > 3, x == 4, !x);
    printf("%d %d %d\n", 6 & 3, 6 | 3, 6 ^ 3);
    printf("%d %d\n", 5 << 2, 20 >> 1);
    int y = 0;
    if (x > 10 && ++y) { }
    printf("y = %d\n", y);
    return 0;
}
```

```text
1 0 0
2 7 5
20 10
y = 0
```

(`++y` never ran because `x > 10` was false — short-circuit.)

## 6. Constants and type casting

```c
#define PI 3.14159      /* macro: text replaced before compiling */
const int MAX = 100;    /* a read-only variable */
int r = (int)9.99;      /* explicit cast → 9 */
```

## Common mistakes

- Forgetting `&` in scanf.
- Expecting `5 / 2` to be 2.5.
- Using `%d` to print a float (garbage output).
- Using `=` instead of `==` in conditions (`if (x = 5)` is always true!).

## Quick recap

- Types: char (1), int (4), float (4), double (8) bytes typically; check with sizeof.
- printf/scanf use format specifiers; scanf needs `&`.
- int/int truncates; % gives the remainder.
- `i++` vs `++i`; never modify a variable twice in one expression.
- Relational results are 1 or 0; && and || short-circuit.


Practice quiz and free certificate: https://skills101.in/learn/c-programming/c-data-types-and-operators