Logical and Arithmetic Operators

What Are Operators in C? With Examples

Operators are symbols such as +, ==, && and ! that act on values. Here are the main groups of operators in C and C++, with a short example program.

On this page
  1. The main groups of operators
  2. An example program
  3. Unary, binary and ternary operators
  4. Operator precedence
  5. Common beginner mistakes
  6. Summary

An operator is a symbol that tells the compiler to do something with one or more values. The values an operator works on are its operands. In 7 + 2, the + is the operator, 7 and 2 are its operands, and the result of the expression is 9.

C and C++ share most of their operators, so the same ideas apply to both. The examples here use C++ and std::cout. Most of the operators work the same way in C.

The main groups of operators

  • Arithmetic: +, -, *, / and % do math on numbers. For the details, see what are arithmetic operators.
  • Comparison: ==, !=, <, >, <= and >= compare two values and give true or false.
  • Logical: && (AND), || (OR) and ! (NOT) combine true and false values. The OR operator in C++ goes deeper into ||.
  • Bitwise: &, |, ^, ~, << and >> work on the individual bits of integers.
  • Assignment: = stores a value in a variable. +=, -= and the others update a variable in one step.
  • Increment and decrement: ++ and -- add one or subtract one. The increment operator in C++ covers them in detail.
  • Conditional: ?: chooses between two values.
  • Pointer: * (dereference) reads the value a pointer points to, and & (address-of) gives the address of a variable.
  • Member access and scope: . and -> reach the members of an object. :: (C++ only) names something inside a namespace or class. The dot operator in c covers the first one.
  • sizeof: sizeof gives the size of a type or object, in bytes.

An example program

This program uses several operators at once:

#include <iostream>

int main() {
    int a = 10;
    int b = 3;

    std::cout << "a + b = " << a + b << "\n";    // 13
    std::cout << "a > b = " << (a > b) << "\n";  // 1, meaning true
    std::cout << "a % b = " << a % b << "\n";    // 1

    bool bothPositive = a > 0 && b > 0;
    std::cout << std::boolalpha << bothPositive << "\n";  // true

    return 0;
}

Output:

a + b = 13
a > b = 1
a % b = 1
true

a + b adds the two numbers. a > b compares them, and the result prints as 1 because std::boolalpha is not set yet. a % b gives the remainder when 10 is divided by 3. The last line combines two comparisons with &&.

Unary, binary and ternary operators

Operators are also grouped by how many operands they take:

  • Operators with one operand, such as -x or !flag, are called unary operators in c.
  • Binary operators take two operands, as in a + b. Most operators are binary.
  • The conditional operator in c, ?:, is the only ternary operator. It takes three operands.

Operator precedence

When an expression has more than one operator, precedence decides which one runs first. Multiplication, division and remainder run before addition and subtraction:

int result = 2 + 3 * 4;  // 14, not 20

Use parentheses to say what you mean. (2 + 3) * 4 is 20.

Common beginner mistakes

  • Using = where you meant ==. if (x = 5) assigns 5 to x and then tests 5, which is true. if (x == 5) compares instead.
  • Using & where you meant &&. & works on bits, while && checks true or false. && also stops early when its left side is false.
  • Expecting a decimal from integer division. 7 / 2 is 3, because both operands are integers. Use 7.0 / 2 to get 3.5.

Summary

An operator is a symbol that acts on its operands. The main groups are arithmetic, comparison, logical, bitwise, assignment, increment, conditional, pointer, member access and sizeof. Learn them one group at a time, and use parentheses whenever precedence might be unclear. Later, you can learn how operator overloading in c lets your own classes use these same symbols.

The complete guide to logical and arithmetic operators

The OR Operator in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Class Constructors in C++

Learn C++ class constructors with compilable examples: default and parameterized forms, member initializer lists, explicit, and initialization order.

Constructors 6 min read

Friend Functions in C++

Learn what a friend function is in C++, how the friend keyword lets a non-member function reach private members, and when to use one, with code examples.