WeCodeAcademy

C Operators

Arithmetic: + - * / %. Comparison: == != < > <= >=. Logical: &&, ||, !. Dividing two integers gives an integer.

ExampleC
#include <stdio.h>

int main(void) {
  int a = 17, b = 5;
  printf("%d\n", a + b);          // 22
  printf("%d\n", a / b);          // 3 (integer division)
  printf("%d\n", a % b);          // 2
  printf("%.1f\n", (double)a / b); // 3.4
  printf("%d\n", a > b && b > 0);  // 1 means true

  int x = 10;
  x += 5;
  x++;
  printf("%d\n", x);
  return 0;
}
Output
22
3
2
3.4
1
16

C has no true/false type in its basic form: a comparison gives 1 for true and 0 for false, and any non-zero value counts as true in an if.

Quick check

What does the comparison 3 > 2 give in C?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.