---
# System prepended metadata

title: ByteWise - Integer Promotion
tags: [' integer', bytewise, ' fml']

---

---
title: ByteWise - Integer Promotion
tags: bytewise, fml, integer
---

# Integer Promotion

Guess what the result is.

```c
#include <stdint.h>
#include <stdio.h>
        
int main() {
    int8_t c1 = 1;                                                                                    
    printf("size of  c1 is %ld\n", sizeof(c1));
    printf("size of -c1 is %ld\n", sizeof(-c1));
} 
```

:::spoiler :boom: The answer is
```shell
size of  c1 is 1
size of -c1 is 4
```
:::

---

This cluase defines what is a **integer promotion**.

> #### § 6.3.1.1 2.) 
> If an **int** can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an **unsigned int**. These are called the **integer promotions**. All other types are unchanged by the integer promotions.

This footnote states when an **integer promotion** is applied.

> #### § 6.3.1.1 2.) [58]
> The integer promotions are applied only: as part of the usual arithmetic conversions, to certain argument expressions, to the operands of the unary +, -, and ~ operators, and to both operands of the shift operators, as specified by their respective subclauses.

:::info
:takeout_box: **Takeaway**:
1. Integral types smaller than `int` are promoted when an operation is performed on them.
2. If all values of the original type can be represented as an `int`, the value of the smaller type is converted to an `int`; otherwise, it is converted to an `unsigned int`.
3. Integer promotions are applied as part of the usual arithmetic conversions to certain argument expressions; operands of the unary +, -, and ~ operators; and operands of the shift operators. The following code fragment shows the application of integer promotions:
:::

Knowing this, let us re-examine our code. When applying an unary opeartor, `-`, to a `char` data, the **integer promotion** occurs. Therefore, `char` is converted to `int` an `int`.

[^1]: https://wiki.sei.cmu.edu/confluence/display/c/INT02-C.+Understand+integer+conversion+rules