Operators (Arithmetic, Logical, Relational, Bitwise, Assignment, Ternary, sizeof())

Operators are C's verbs: arithmetic computes, relational compares (== asks, = assigns), logical combines conditions, ternary picks in one line, and sizeof measures boxes.

11 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Boxes full, now do something

The marks program stores three subject marks: 78, 65, 92. Storage was last lesson. Today the accountant work: total, percentage, and the question every student actually asks, did I pass?

Every one of those is an operator at work. Operators are the verbs of C, and like verbs in any language, two that look almost identical (= and ==) mean completely different things, and confusing them has crashed real payroll runs.

At a glance

The operator families

FamilyOperatorsOne example
Arithmetic+ - * / %total = m1 + m2 + m3
Relational< > <= >= == !=marks >= 35 gives 1 or 0
Logical&& || !pass in both subjects: a && b
Assignment= += -= *= /=marks += 5 (grace marks)
Ternary? :result = (m >= 35) ? 'P' : 'F'
Bitwise& | ^ << >>works on individual bits
sizeof()sizeof(int)size of a box in bytes: 4

Theory

Arithmetic's two famous surprises

Surprise 1, integer division: with int boxes, 235 / 3 is 78, not 78.33. Two ints divide into an int; decimals are discarded (truncation again). Want the decimals? Make one side floating: 235 / 3.0 gives 78.33.

Surprise 2, the % operator: % is modulus, the REMAINDER after division: 7 % 2 is 1, 10 % 5 is 0. It has nothing to do with percentages, despite the symbol. (Odd/even tests, cyclic rolls, and half the exam's tricky questions run on %.)

Practical

The marks program computes

#include <stdio.h>

int main() {
    int m1 = 78, m2 = 65, m3 = 92;
    int total = m1 + m2 + m3;
    float percentage = total / 3.0;   /* 3.0, not 3 */
    char result = (percentage >= 35) ? 'P' : 'F';

    printf("Total: %d\n", total);
    printf("Percentage: %f\n", percentage);
    printf("Result: %c\n", result);
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Read the ternary like a sentence

The line result = (percentage >= 35) ? 'P' : 'F'; is the ternary operator. Read it aloud as an English sentence first, then say what result holds for percentage 78.33.

Show the answer

"IS percentage at least 35? If yes, take 'P'; otherwise take 'F'." For 78.33 the condition is true, so result holds 'P'.

The ternary is a one-line if-else: condition ? valueIfTrue : valueIfFalse. Exams love asking its output for a given value, and its name: the only C operator taking three operands.

Quiz

In C, what is the value of 7 % 2, and separately, of 7 / 2 (both ints)?

  1. 7 % 2 is 1, and 7 / 2 is 3
  2. 7 % 2 is 3.5, and 7 / 2 is 3.5
  3. 7 % 2 is 3, and 7 / 2 is 1
  4. 7 % 2 is 1, and 7 / 2 is 3.5
Show the answer

7 % 2 is 1, and 7 / 2 is 3

Integer division 7 / 2 keeps only the whole part: 3. Modulus 7 % 2 keeps only the remainder: 1. Together they split any division into quotient and remainder. Option C swaps them, the standard trap, and any option with 3.5 forgot both sides are ints.

Quiz

A student writes if (marks = 100) intending to check for a perfect score. What actually happens?

  1. marks is ASSIGNED 100, and the condition is always true
  2. C compares marks with 100 as intended
  3. The compiler always rejects it as an error
  4. The condition is always false
Show the answer

marks is ASSIGNED 100, and the condition is always true

= assigns: marks becomes 100, destroying its real value, and the expression's value (100, nonzero) counts as TRUE every single time. Most compilers accept it silently. The comparison needed ==. Burn the mantra in: = assigns, == asks. This single character has caused real-world outages.

Watch out

The three mark-eaters

= vs == (assign vs compare, above). Integer division: total / 3 with ints silently drops decimals; write 3.0 or use float. Relational results: in C a comparison yields 1 or 0, not "true/false" words, so printf("%d", 5 > 3) prints 1, a favourite predict-the-output question. Bitwise operators exist (& | ^ << >>) and work on bits; at this stage exams want their NAMES, not mastery.

Theory

sizeof: the measuring tape

sizeof(int) answers "how many bytes is this box?", typically 4, and sizeof(marks) measures a variable. It looks like a function but is an operator, exams ask exactly that. You will meet it again with arrays and pointers, where measuring boxes becomes daily work. Next lesson: the toolbox lines at the top of every program, #include and #define, finally explained.

Summary

Key takeaways

  • Operator families: arithmetic, relational, logical, assignment, ternary, bitwise, sizeof.
  • Integer division truncates (7/2 is 3); % gives the remainder (7%2 is 1).
  • = assigns, == asks; if (marks = 100) assigns and is always true.
  • Relational and logical results are 1 (true) or 0 (false).
  • Ternary: condition ? ifTrue : ifFalse, C's only three-operand operator.
  • sizeof is an operator that reports a box's bytes.
  • Memory hook: operators are C's verbs; = assigns, == asks.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to C language

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati