Categories
c programming tips and tricks

Remember assignment operators in the C language

Though it’s nice to write readable code, one handy tool in the C language is an assignment operator. Even if you don’t use one, you need to be able to recognize it.

The following equation is quite common in programming:

a = a + n;

In C, you can abbreviate this statement by using an assignment operator:

a += n;

The operator goes before the equal sign. If it went afterward, it might change into a unary operator, which looks weird:

a =+ n;

So the value of variable a equals positive n? The compiler may buy that argument, but it’s not what you intended.

Also, don’t forget the increment and decrement operators, ++ and --, which are quite popular in loops.

Leave a Reply

Your email address will not be published. Required fields are marked *