Wednesday 20 September 2017

C Programming for Beginners: Operators

This is part 4 of my series on C programming for beginners. (See also part 3)

In your programs you will often want to assign values to variables and, later on, test those values. For example, you might write a program in which you test the age of an employee in order to calculate his or her bonus. Here I use the ‘greater than’ operator > to test of the value of the age variable is greater than 45:

if (age > 45) {
bonus = 1000;

Operators are special symbols that are used to do specific operations such as the addition and multiplication of numbers. One of the most important operators is the assignment operator, =, which assigns the value on its right to a variable on its left. Note that the type of data assigned must be compatible with the type of the variable. This is an assignment of an integer (10) to an int variable named myintvariable:

int myintvariable;
myintvariable = 10;


Beware. While one equals sign = is used to assign a value, two equals signs == are used to test a condition.

= this is the assignment operator.
e.g. x = 1;

== this is the equality operator.
e.g. if (x == 1) 


C can perform tests using the if statement. The test itself must be contained within parentheses and it should be capable of evaluating to true or false. If true, the statement following the test executes. Optionally, an else clause may follow the if clause and this will execute if the test evaluates to false. Here is an example:

if (age > 45) {
bonus = 1000;
} else {
bonus = 500;

You may use other operators to perform other tests. For example, this code tests if the value of age is less than or equal 70. If it is, then the conditional evaluates to true and "You are one of our youngest employees!"  is displayed. Otherwise the condition evaluates to false and nothing is displayed:

if (age <= 70){
printf("You are one of our youngest employees!\n");

Notice that the <= operator means ‘less than or equal to’. It performs a different test than the < operator which means ‘less than’. These are the most common comparison operators that you will use in tests:

== // equals
!= // not equals
> // greater than
< // less than
<= // less than or equal to
>= // greater than or equal to


Some assignment operators in C perform a calculation prior to assigning the result to a variable. This table shows some examples of common ‘compound assignment operators’ along with the non-compound equivalent.

operator                    example                       equivalent to
+= a += b a = a + b
-= a -= b a = a - b
*= a *= b a = a * b
/= a /= b a = a / b

It is up to you which syntax you prefer to use in your own code. Many C and C++ programmers prefer the terser form as in: a += b. But the same effect is achieved using the slightly longer form as in: a = a + b.

I’ll explain prefix and postfix operators in the next article. And if you want to learn C in more depth, why not sign up to my online video course – C Programming for beginners. See here: