Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing float values in C program gets stuck

Tags:

c

I am working on an assignment for an embedded software course, but I'm having the strangest problem.

Using the code below:

void decidePotato(float held)
{
    printf("Deciding Potato, held for %f seconds \n", held);
    if (held >= 1.99)
    {
        printf("Held for more than 1.99s \n", held);
        returnPotato();
    }
    printf("I evaluated the if statement above \n");

}

I get the following output:

Deciding Potato, held for 0.010000 seconds

I dont even see the "I evaluated the if statement above" message, so the program somehow got stuck evaluating that if statement. And it remains stuck until I reprogram the board How is that even possible?


1 Answers

I suggest to put a fflush() at the end of your function: even if with the new line you should force printing, it might be that your compiler has a "strange" implementation...

By the way: are you redirecting your output to a file? Because in that case this could apply.

Anyway, as Scotty Bauer noticed, you have to correct the printf within the if block: you probably also got a compiler warning for that.

Note from fflush manual:

If the stream argument is NULL, fflush() flushes all open output streams.

Normally you would do fflush(stdout).

like image 108
Antonio Avatar answered May 23 '26 02:05

Antonio