# Thread: Explain what can go wrong with the following C code

1. ## Explain what can go wrong with the following C code

Please help me to explain what can go wrong with the following C code, why it looping forever? I don't understand.

Code:
```#include "stdafx.h"
#include <stdio.h>

int main()
{
int x;
float y;
x = 1;
while (x > 0)
{
y = x+1;
x = y;
}
return 0;
}```
Regards. 2. 3. Originally Posted by Elysia Oh, Eysia. I'm a newbie, so I have learnt C for 2 week in this summer-semester. I have read your recommended document so I don't understand clearly. Can you detail my problem, many thanks to you! 4. In short: there isn't enough storage in a float to exactly store all kinds of numbers. Remember that a float has to store decimals as well as the integer part.
Eventually, you will stumble upon a number that cannot be exactly represented in a float. It happens that two specific numbers are stored in the same way because it cannot be represented exactly (again, lack of enough storage).
At some point, x and x + 1 are the same number in a float. Then you hit your ceiling and cannot proceed. 5. Code:
`	while (x > 0)`
x is always > 0? 6. It will eventually overflow, resulting in a negative number. 7. Originally Posted by luckyluke Please help me to explain what can go wrong with the following C code, why it looping forever? I don't understand.

Code:
```#include "stdafx.h"
#include <stdio.h>

int main()
{
int x;
float y;
x = 1;
while (x > 0)
{
y = x+1;
x = y;
}
return 0;
}```
Regards.
The while loop will never break out, because you set x equal to 1, then you increase x by 1, in every iteration of the loop (using y as an intermediary).

So how is x ever going to be anything else but greater than zero? 8. Of course, it's also a question if a float can overflow? 9. Originally Posted by Elysia In short: there isn't enough storage in a float to exactly store all kinds of numbers. Remember that a float has to store decimals as well as the integer part.
Eventually, you will stumble upon a number that cannot be exactly represented in a float. It happens that two specific numbers are stored in the same way because it cannot be represented exactly (again, lack of enough storage).
At some point, x and x + 1 are the same number in a float. Then you hit your ceiling and cannot proceed.
I have just understood, many thanks to you! Popular pages Recent additions 