← Back to C Course | Chapter 3: Operators | Lesson 1 of 9

C Operators

Operators are symbols that act on one or more operands to produce a new value, and C groups them into categories -- arithmetic, relational, and logical -- based on the kind of result they produce.
Syntax
c
result = operand1 operator operand2;

// operator: arithmetic (+ - * / %), relational (== != < >), logical (&& || !)

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?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare sum and set it to 3 + 4
	int sum = 3 + 4;
	// Print formatted text to the screen
	printf("%d", sum);
	// Return 0 to signal that the program finished successfully
	return 0;
}

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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare sum and set it to 3 + 4
	int sum = 3 + 4;
	// Declare isGreater and set it to sum > 5
	int isGreater = sum > 5;
	int result = (sum > 5) && (sum < 10);
	// Print formatted text to the screen
	printf("%d %d %d", sum, isGreater, result);
	// Return 0 to signal that the program finished successfully
	return 0;
}

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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 5, b = 3;
	// Declare sum and set it to a + b
	int sum = a + b;
	// Declare neg and set it to -a
	int neg = -a;
	// Print formatted text to the screen
	printf("%d %d", sum, neg);
	// Return 0 to signal that the program finished successfully
	return 0;
}

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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare score and set it to 75
	int score = 75;
	// Check whether score + 5 > 70
	if (score + 5 > 70) {
		// Print formatted text to the screen
		printf("Passed");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare result and set it to 2 + 3 * 4
	int result = 2 + 3 * 4;
	// Print formatted text to the screen
	printf("%d", result);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Confusing = (assignment) with == (comparison).
  2. Expecting 7 / 2 to give 3.5, when integer division gives 3.
  3. Assuming a + b * c is evaluated left to right, when * has higher precedence.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.