++/– Operators work on floats and doubles

laptop computer with an editor
From Instant Images

No matter how much I use C there are still things I thought I knew but find out otherwise. The latest one is that you can use ++ or — on float and double variables.

Here for instance is a short nonsense program that I compiled and ran with Visual Studio on Windows. As you’#ll notice, it increments and decrements floats and a double.

The danger with using this is that you might get rounding errors. When I ran it on Windows, I didn’t which surprised me. I will have to try this on Linux.

#include <stdio.h>

void main()
{
    float c = 1.0f;
    float cin = 10000.0f;
    double d = 2.0;
    int i = 0;

    while (cin != c)
    {
        i++;
        cin = c;
        c--;
        d++;
    }
    printf("%i %e %e\n", i, c, d);
}
(Visited 11 times, 1 visits today)