---
# System prepended metadata

title: Specified Formatted Output with `printf`

---

{%hackmd @themes/orangeheart %}

# Specified Formatted Output with `printf`

## Conversion Specifiers

### Integers

| Conversion Specifier | Description | Example | Output |
| - | - | - | - |
| `d` | **signed decimal** | `printf("%d", 455)` | `455`
| `i` | **signed decimal** (different when used with `scanf`) | `printf("%i\n", 455);` | `455`
| `o` | **unsigned octal** | `printf("%o\n", 455);` | `707`
| `u` | **unsigned decimal** | `printf("%u\n", -455);` | `4294966841`
| `x` | **unsigned hexadecimal: 0-9 and *lowercases*** | `printf("%x\n", 455);` | `1c7` 
| `X` | **unsigned hexadecimal: 0-9 and *uppercases*** | `printf("%X\n", 455);` | `1C7`
| `h`, `l` or `ll` | placed **before** any specifier to indicate `short`, `long` or `long long` (length modifiers) | `printf("%ld\n", 2000000000L);` | `20000000000`

### Floating-points

| Conversion Specifier | Description | Example | Output |
| - | - | - | - |
| `e` or `E` | **exponential notation** | `printf("%e\n", 1234567.89);` | `1.234568e+006`
| `f` or `F` | **fixed-point notation** | `printf("%f\n", 1234567.89);` | `1234567.890000`
| `g` or `G` | display exponential or fixed-point form based on the **magnitude of the value** | `printf("%g\n", 1234567.89);` | `1.23457e+006`
| `L` | indicate the placing of a `long double` floating-point | - | - 

### Others

| Conversion Specifier | Description | Example | Output |
| - | - | - | - |
| `p` | **pointer values** | `printf("%p\n", xptr);` | `000000000061FE14`
| `%` | display `%` | `printf("%%\n");` | `%`

## Flags and Justifying

```C
int x = 42;
float y = 3.14159;

printf("%+d\n", x);      // prints "+42" (include plus sign for positive numbers)
printf("%#x\n", x);      // prints "0x2a" (include prefix for hexadecimal notation)
printf("%-10f\n", y);    // prints "3.14159     " (left-justify within field width of 10)
printf("%010d\n", x);    // prints "0000000042" (pad with zeros instead of spaces)
```

- `+`: include positive sign on numbers
- `#`: include prefix on hexadecimal notations
- `-10f`: **left-justify within field width of 10**
- `%010d` or `%.10d`: **pad with zeros instead of spaces**

Output:

```
+42
0x2a
3.141590
0000000042
```

**Right Justifying:**

```C
printf("%4d\n", 1);
printf("%4d\n", 12);
printf("%4d\n", 123);
printf("%4d\n", 1234);
printf("%4d\n", 12345);
```

This can be understood as **leaving 4 spaces from the left for the output.**

Output: 

```
   1
  12
 123
1234
12345
```

## Field Widths

**String Precisions:**

```C
printf("%.11s\n", "merry christmas");
```

`%.11s` prints the first 11 characters of the string:

```
merry chris
```

**Float Precision:**

```C
printf("%5.2f", 3.12345);
```

- `%5.2f` rounds the float to 2 decimal points and right justifies by a field width of 5

Output:

```
 3.12
```

**Left vs Right Justifying:**

```C
puts("1234567890123456789012345678901234567890");
printf("%10s%10d%10c%10f\n", "hello", 7, 'a', 1.23);
printf("%-10s%-10d%-10c%-10f", "hello", 7, 'a', 1.23);
```

Output:

```
1234567890123456789012345678901234567890
     hello         7         a  1.230000
hello     7         a         1.230000
```

