SovranCode
HomeCourses C C Operators
This device
Course contentsC Operators · 21 titles

C Fundamentals

C IntroductionC First ProgramC VariablesC Data TypesC OperatorsC Input and Output

Control Flow

C If ElseC For LoopC While Loop

Functions and Collections

C FunctionsC ArraysC Strings

Memory and User-Defined Types

C PointersC StructuresC EnumsC Dynamic Memory

Files, Tooling, and Projects

C File HandlingC PreprocessorC DebuggingC MakefilesC Project: Command-Line Inventory Manager
Learn C Programming21 complete lessons

C Fundamentals

C IntroductionC First ProgramC VariablesC Data TypesC OperatorsC Input and Output

Control Flow

C If ElseC For LoopC While Loop

Functions and Collections

C FunctionsC ArraysC Strings

Memory and User-Defined Types

C PointersC StructuresC EnumsC Dynamic Memory

Files, Tooling, and Projects

C File HandlingC PreprocessorC DebuggingC MakefilesC Project: Command-Line Inventory Manager
PREVIOUS LESSONC Data Types
NEXT LESSONC Input and Output
C Fundamentals 45 min

C Operators

Calculate and compare values with operators whose types, order, and side effects you can explain.

What you will leave with

You will be able to trace + - * / %, assignment, increment, comparison, and logical operators, explain integer division, and keep mixed expressions readable with parentheses or named intermediates.

Arithmetic operators transform numeric values

Multiplication, division, and remainder happen before addition and subtraction. When both operands of / or % are integers, the result is an integer: 17 % 5 is 2, and 7 / 2 is 3. If a fraction matters, make at least one operand floating-point, as C Data Types explains.

+   -

Add and subtract

Combine quantities or calculate a difference.

*   /

Multiply and divide

The types of both operands decide whether division keeps a fraction.

%

Integer remainder

Defined for integers in this course. Do not treat it as a floating-point operator.

C FUNDAMENTALS RUNNER

Calculate groups and a remainder

Change the chapter count or group size. Predict total_pages, whole_groups, and spare_pages before running.

#include <stdio.h>

int main(void) {
  int pages_per_chapter = 8;
  int chapters = 3;
  int total_pages = pages_per_chapter * chapters;
  int whole_groups = total_pages / 5;
  int spare_pages = total_pages % 5;

  printf("Total pages: %d\n", total_pages);
  printf("Groups of five: %d, spare: %d\n", whole_groups, spare_pages);
  return 0;
}
TERMINAL OUTPUT
Edit the program, predict its output, then run it.

This safe browser runner supports the lesson subset: scalar and fixed-size int/double arrays, character strings, indexed reads and numeric writes, arithmetic, printf/puts, numeric scanf into scalar or bounded array-element addresses, comparisons, bounded if/else, for/while loops, and return.

Try this next: Change the group size from 5 to 6 and explain the new quotient and remainder.

Assignment stores; increment updates by one

= is not a math claim. It evaluates the right side, then stores the result in the left-side object. Compound forms such as += and -= are shorthand for “read, calculate, store.” Prefix ++n updates then yields the new value; postfix n++ yields the old value then updates. In beginner code, n = n + 1; is often the clearest form.

C FUNDAMENTALS RUNNER

Trace a reassignment

Run once, then change the amount added to checked_in.

#include <stdio.h>

int main(void) {
  int checked_in = 12;

  checked_in = checked_in + 1;
  printf("%d learners have checked in.\n", checked_in);
  return 0;
}
TERMINAL OUTPUT
Edit the program, predict its output, then run it.

This safe browser runner supports the lesson subset: scalar and fixed-size int/double arrays, character strings, indexed reads and numeric writes, arithmetic, printf/puts, numeric scanf into scalar or bounded array-element addresses, comparisons, bounded if/else, for/while loops, and return.

Try this next: Add a second assignment that subtracts one cancellation, then print the final count only once.

Comparison and logical operators produce conditions

Equality needs two characters: == compares, = assigns. Relational operators are < <= > >=. Logical && requires both sides, || accepts either, and ! inverts. C treats zero as false and nonzero as true. Using these operators inside if is the job of C If Else; this lesson is the catalog of the operators themselves.

C FUNDAMENTALS RUNNER

Combine a comparison and a logical AND

Change score to 70, 69, and 0. Predict the message before each run.

#include <stdio.h>

int main(void) {
  int score = 82;
  int passing = 70;

  if (score >= passing && score != 0) {
    puts("Ready to report.");
  } else {
    puts("Check the score.");
  }
  return 0;
}
TERMINAL OUTPUT
Edit the program, predict its output, then run it.

This safe browser runner supports the lesson subset: scalar and fixed-size int/double arrays, character strings, indexed reads and numeric writes, arithmetic, printf/puts, numeric scanf into scalar or bounded array-element addresses, comparisons, bounded if/else, for/while loops, and return.

Try this next: Replace && with || and explain which scores now print Ready to report.

Bitwise operators & | ^ ~ << >> exist in C. This course uses them only when a later systems topic needs them; do not treat them as a substitute for && and ||.

Make grouping visible

C evaluates ! before comparisons, comparisons before &&, and && before ||. Arithmetic * / % bind tighter than + -. You do not need to rely on that table for a complicated rule: parentheses or a named intermediate value make the intended grouping obvious.

Side effects belong in their own statement

Do not write n++ * n++ or mix increment with other uses of the same object in one expression. The result can be undefined or merely unreadable. Update the variable, then use it.

Compile mixed expressions with conversion warnings

macOSKeep integer versus floating-point conversions visible.clang -std=c11 -Wall -Wextra -Wconversion operators.c -o operators
LinuxBuild with GCC or Clang.gcc -std=c11 -Wall -Wextra -Wconversion operators.c -o operators
WindowsUse /W4 and inspect every conversion diagnostic.cl /W4 operators.c

Independent lab: one named calculation

Write a program that computes a total with *, a group count with /, and a remainder with %, then prints whether the remainder is zero using ==. Predict each line before you run it.

Lesson review

  • Arithmetic, assignment, increment, comparison, and logical operators are separate jobs.
  • Integer / and % stay in the integer domain.
  • == compares; = stores.
  • Parentheses or named intermediates beat a memorized precedence table.

Related lessons

  • C Data Types — Operand types decide integer versus floating-point results.
  • C If Else — Comparisons become branches.
KNOWLEDGE CHECK

Read the operator before the result

Name the operands, the operator, and whether it stores a new value or only computes one.

01What is the value of 17 % 5 when both operands are int?
02What does ++n do when n is an int variable?
03Which operator compares equality instead of assigning?
04What does && mean?
05Why add parentheses to a mixed arithmetic expression?
PREVIOUS LESSONC Data Types
NEXT LESSONC Input and Output
ON THIS PAGEC OperatorsArithmeticAssignment and incrementComparison and logicPrecedenceCompile locallyIndependent labLesson reviewKnowledge checkRelated lessons
Course contents