# Integer Promotion and Normal Arithmatic in C

## Integer Promotion

Integer promotion is a concept in C where

**smaller integer types are automatically promoted to larger integer**types before certain operations are performed.It is a mechanism used by the C language to

**ensure consistent behavior**and**prevent data loss**or unexpected results in certain situations.The integer promotion rules specify that any integer type smaller than an int is promoted to an int or unsigned int before performing certain operations.

These operations include arithmetic operations, bitwise operations, and comparisons.

The purpose of integer promotion is to avoid the loss of precision that could occur if the operations were performed directly on smaller integer types.

### Example

Consider an example of adding a character with an integer

`#include <stdio.h>`

main() {

int i = 17;

char c = 'c'; /* ascii value is 99 */

int sum;

sum = i + c;

printf("Value of sum : %d\n", sum );

}

**Explanation**

- When the above code is compiled and executed, it produces the following result:
- Value of sum : 116
- Here, the value of sum is 116 because the compiler is doing integer promotion and converting the value of 'c' to ASCII before performing the actual addition operation.

### Key points regarding integer promotion:

**Smaller Integer Types:**

- Integer types smaller than int, such as char and short, are subject to promotion.
- This means that when these types are involved in expressions or operations, they are automatically converted to an int or unsigned int.

**Default Promotion:**

- The default integer promotion is to promote smaller integer types to int if int can represent all the values of the original type.
- If int is not sufficient, the promotion is to unsigned int.

**Signedness Preservation:**

- Integer promotion preserves the signedness of the original type.
**For example:**A signed char will be promoted to a signed int, while an unsigned char will be promoted to an unsigned int.

**Arithmetic Operations:**

- When performing arithmetic operations involving smaller integer types, the types involved are promoted to a common type based on the rules of integer promotion.
- This ensures that the result of the operation is consistent and maintains the desired precision.

**Bitwise Operations:**

- Similar to arithmetic operations, bitwise operations such as AND, OR, and XOR also follow the rules of integer promotion.
- The types involved in the bitwise operation are promoted to a common type before the operation is performed.

## Normal Arithmetic Conversion

Normal arithmetic conversion, also referred to as usual arithmetic conversions, is a process in C that determines the

**common type to which two operands should be converted before performing arithmetic or logical operations**on them.It ensures that the operands are of compatible types, allowing for consistent and predictable behavior during calculations.

The normal arithmetic conversion rules are applied when performing operations between operands of different types, such as mixing integer types with floating-point types or mixing different integer types.

The rules dictate that the operands are converted to a common type based on a hierarchy of types.

### Key points about normal arithmetic conversion:

**Hierarchy of Types:**

- C defines a hierarchy of types based on their size and signedness. The hierarchy,
**from lowest to highest**, is as follows: float, double, long double. - On the integer side, it is: char, short, int, long, long long. The implementation may have variations, but the general idea is to find a type that can represent the range and precision of both operands.

**Promotion and Conversion:**

- If the operands have different types, the one with the lower rank in the hierarchy is promoted to the type of the other operand.
- If there is still a mismatch, integer types are converted to floating-point types if necessary, giving preference to double or long double over float.
- The result of the conversion is the common type to which both operands are converted.

**Precision Preservation:**

- During the conversion, C tries to preserve the precision of the original operands as much as possible.
- However, it's important to note that some loss of precision may occur if the operands have different sizes or representations.

**Signedness Consideration:**

- The rules also take into account the signedness of the operands.
- If the operands have different signedness, the conversion follows specific rules to determine the resulting signedness of the common type.