what happens when we exceed valid range of built-in data types in C.

Hello coders! Welcome back!

So, you might be wondering what will happen if we exceed the valid range of a given data type.

Let's look into an example-



















We know the size of an unsigned integer is 4294967295. I have increased it by one, the value feed as input is 4294967296. So the value the compiler is going to return is 0.

Let's look into the explanation of why this is happening.

To explain I take a simpler number. let's take 3-bit representation. We know that the minimum value is zero and the maximum number is 7. If I want to display 8 let's see what will happen.



















So to represent the decimal number 8 we need 4 bits. But in the example we are taking only 3 bits, so the value it's going to return is 0;

If we want to give input as 9, the compiler will return 1.
The binary representation of number 9 is - 1001
Since we are taking only 3 bits so it will hold 001, the value is 1. And the compiler will return value 1.

Imagine this process as a clock! The clock moves its hour hand to 1 after 12.
Similarly, the compiler returns the minimum value after reaching the maximum value.

Hopes so, you got a clear idea about what will happen when we exceed the maximum value. If any queries drop a comment. always ready to clear them.

Happy learning! 

Comments

Popular post