CodeBlanks Logo Icon CodeBlanks

Summary - Operators

Combine arithmetic, comparison and bitwise operators to build a grade calculator.

Summary: Operators

In this group you saw five operator families:

  • Arithmetic: + - * / %
  • Assignment: = += -= ++ --
  • Comparison: == != < > <= >=
  • Logical: && || !
  • Bitwise: & | ^ ~ << >>

Comparison and logical operators return 1 or 0. Bitwise operators work on the bits of numbers. Let's combine them in a short exercise.

Practice Mixed operator exercise

Define int a = 10, b = 3;. With four printf calls print a + b, a % b, a > b and a & b on separate lines. Output should be 13, 1, 1, 2.

Starter code

main.c
#include <stdio.h>

int main(void) {
    int a = 10, b = 3;
    /* print a + b, a % b, a > b, a & b */
    return 0;
}

Sample solution

main.c
#include <stdio.h>

int main(void) {
    int a = 10, b = 3;
    printf("%d\n", a + b);
    printf("%d\n", a % b);
    printf("%d\n", a > b);
    printf("%d\n", a & b);
    return 0;
}

Expected output

13
1
1
2

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 After x = 5; x += 3; what is x?

  • 3
  • 8 Correct answer
  • 5
  • 15

Explanation: x += 3 means x = x + 3; 5 + 3 = 8.

2 Increment operator

Question
int x = 5;
x____;
/* x is now 6 */
Answer
int x = 5;
x++;
/* x is now 6 */

Explanation: Answer is ++. x++; makes x 6.

3 Match operator families

Term Meaning
+ arithmetic
== comparison
&& logical
& bitwise

All lessons in Beginner