Tag: operators

++/– Operators work on floats and doubles

++/– 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);
}
Useful reference to C operators

Useful reference to C operators

Reference
Image by ElasticComputeFarm from Pixabay

Ever wondered what all the operators that you can use = are. Like ^=. Or what about operator precedence? Does * come before ++? (Answer no, ++ is higher precedence than *).

Do you know how

a & b == 7

is parsed? It’s actually a & (b==7)

This Wikipedia page lists all the operators with precedence order and as it includes C++, it lets you see what you can and can’t do in C and C++.  It’s worth bookmarking whenever you need to look these things up.