C Operators
In this page:
What are Operators?
An operator is a special symbol that performs an operation on one or more values, called operands, producing a new value -- the + symbol, for example, adds two numeric operands together.
Example: What are Operators?
#include <stdio.h>
int main() {
int sum = 3 + 4;
printf("%d", sum);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Categories of Operators in C
C groups operators into categories based on what kind of result they produce: arithmetic operators compute numbers, relational operators compare values, and logical operators combine truthy or falsy results.
Example: Categories of Operators in C
#include <stdio.h>
int main() {
int sum = 3 + 4;
int isGreater = sum > 5;
int result = (sum > 5) && (sum < 10);
printf("%d %d %d", sum, isGreater, result);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Operators and Operands
Every operator acts on operands -- most operators like + and * are binary and need two operands, while a few like ++ and unary - are unary and act on just a single operand.
Example: Operators and Operands
#include <stdio.h>
int main() {
int a = 5, b = 3;
int sum = a + b;
int neg = -a;
printf("%d %d", sum, neg);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Combining Operators in Expressions
Operators of different categories are frequently combined in a single expression, such as mixing arithmetic and relational operators to build a condition used inside an if statement.
Example: Combining Operators in Expressions
#include <stdio.h>
int main() {
int score = 75;
if (score + 5 > 70) {
printf("Passed");
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Operator Precedence Preview
When an expression mixes multiple operators, C evaluates them according to a fixed precedence order -- multiplication and division run before addition and subtraction by default, and parentheses can always override that default order.
Example: Operator Precedence Preview
#include <stdio.h>
int main() {
int result = 2 + 3 * 4;
printf("%d", result);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: