Operators

Now that you have explored some of the basic elements of the C programming language, you can begin looking at operators. Operators allow you to manipulate one or more value, and the result can be stored or further manipulated to your liking. You have already learned about the assignment operator, =, but there are additional operators that are essential for a C programmer to know.

Example Program

#include <stdio.h>

int main()
{
    int a = 12;
    int b = 3;
    
    printf("\nArithmetic Operators:\n");
    printf("a + b = %d\n", a + b);
    printf("a - b = %d\n", a - b);
    printf("a * b = %d\n", a * b);
    printf("a / b = %d\n", a / b);
    printf("a %% b = %d\n", a % b);
    printf("a++ = %d\n", a++);
    printf("++b = %d\n", ++b);
    printf("a-- = %d\n", a--);
    printf("--b = %d\n", --b);
    
    printf("\nRelational Operators:\n");
    printf("a == b = %d\n", a == b);
    printf("a != b = %d\n", a != b);
    printf("a >= b = %d\n", a >= b);
    printf("a > b = %d\n", a > b);
    printf("a <= b = %d\n", a <= b);
    printf("a < b = %d\n", a < b);
    
    printf("\nLogical Operators:\n");
    printf("!1 = %d\n", !1);
    printf("1 && 1 = %d\n", 1 && 1);
    printf("1 || 1 = %d\n", 1 || 1);
    
    return 0;
}

If you compile and run this code, you will see the following output:

Arithmetic Operators:
a + b = 15
a - b = 9
a * b = 36
a / b = 4
a % b = 0
a++ = 12
++b = 4
a-- = 13
--b = 3

Relational Operators:
a == b = 0
a != b = 1
a >= b = 1
a > b = 1
a <= b = 0
a < b = 0

Logical Operators:
!1 = 0
1 && 1 = 1
1 || 1 = 1

Printf and Integer Types

Another commonly used function from stdio is printf, which allows you to format and print strings to the console. Something worth noting about our example is that we use “\n” in each statement. The reason for this is that this is a special character, called a newline character, which causes the console to go to the next line after it is printed. Up until now, we have been using puts, which adds a newline automatically. Another thing to notice is the use of %d, which indicates to the printf function that we would like it to insert an integer at that place in the string. As you will learn later on, there are different things you can put after %, but in this case since our variables a and b are int data types, we use %d.

Arithmetic Operators

Even before the invention of C, one of the most obvious benefits of computers was the ability to quickly and accurately perform calculations on large sets of numbers. Knowing this, the inventors of C included a set of operators to assist with the most fundamental arithmetic calculations. Note that since we are using integers, all decimals are ignored. So 7 / 2 is going to be 3, even though mathematically it is 3.5.

printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
printf("a++ = %d\n", a++);
printf("++b = %d\n", ++b);
printf("a-- = %d\n", a--);
printf("--b = %d\n", --b);

The first four operators you should recognize from your math class, so I will focus on the last 5. The first one is called a modulo operator, and it will calculate the remainder of a / b. The next two are increment operators, which are indicated by ++ before or after the identifier. The last two are decrement operators, –. These operators are intended to increase or decrease a number by 1. If you look closely at the output from the example program, you will notice that putting the operator before the identifier, called a prefix, causes it to calculate the value before printing it out, while putting the operator after the identifier, called a postfix, causes it to calculate the value after printing it out.

Relational Operators

When searching or sorting through data, it is helpful to be able to analyze different values in relation to each other. For that, we have relational operators.

printf("a == b = %d\n", a == b);
printf("a != b = %d\n", a != b);
printf("a >= b = %d\n", a >= b);
printf("a > b = %d\n", a > b);
printf("a <= b = %d\n", a <= b);
printf("a < b = %d\n", a < b);

In order they order of our example program, they are equal, not equal, greater than or equal, greater than, less than or equal, and less than. The notable thing about how these operators are handled in C is that they will always evaluate to either 0 or 1, for false and true, respectively. In many other languages there is a boolean data type, which can hold true or false. But not in C, which expects you to be smart enough to understand that behind the scenes, a boolean is just an integer with a value 0 or 1. It will be more obvious how useful these are later, so understanding only the basics is sufficient for now.

Logical Operators

The last type of operator you will learn about is called a logical operator. Like a relational operator, the resulting value is either 0 or 1, but unlike a relational operator, the input values are also usually 0 or 1.

printf("!1 = %d\n", !1);
printf("1 && 1 = %d\n", 1 && 1);
printf("1 || 1 = %d\n", 1 || 1);

The first operator shown is called a negation, or a not. Basically, whatever you pass to it is flipped, so 0 to flipped to 1 and 1 is flipped to 0. The second operator is an and operator, and it takes two values. If both of the values are not 0, it will return a 1. The last operator is a or, which also takes two values. The or operator will return 1 if at least one of the two values passed to it is not 0.

<< prev | next >>

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x