C Operators
Operators are special symbols that tell C to perform an action on one or more values. For example, the + symbol adds two numbers together. Operators are the building blocks of almost every calculation and decision your program will ever make, so it is worth getting comfortable with them early.
What Are Operators?
In the expression 5 + 3, the plus sign is the operator and the numbers 5 and 3 are called operands. C runs the operator on the operands and gives you back a result, in this case 8.
Using an operator
#include <stdio.h>
int main() {
int sum = 5 + 3;
printf("5 + 3 = %d\n", sum);
return 0;
}Groups of Operators
C divides operators into several groups depending on the job they do. In this topic we will focus on the four groups you will use the most as a beginner.
Comparison and logical operators produce a result that is either true or false. In C, false is represented by the number 0, and true is represented by 1.
Mixing operator groups
#include <stdio.h>
int main() {
int age = 20;
int isAdult = age >= 18; // comparison gives 1 (true)
printf("Age: %d\n", age);
printf("Is adult? %d\n", isAdult);
return 0;
}- Arithmetic operators work with numbers to give a numeric result.
- Assignment operators put a value into a variable.
- Comparison and logical operators give a result of 1 (true) or 0 (false).
Exercise: C Operators
What is required to use the % (modulus) operator in C?