HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to CVariablesData TypesOperatorsStringsArraysConditions (if / switch)Loops (for / while)FunctionsPointers
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
👉 Pointer
C›Operators

Operators

Math (+ - * / %), comparison (== < >), and logic (&& || !).

Simple enough for a 5-year-old to understand

Math

Example code
1
#include <stdio.h>
2
int main() {
3
printf("%d\n", 7 % 2);
Remainder: 1
CodeRemainder: 1
4
printf("%d\n", 7 / 2);
Integer division: 3
CodeInteger division: 3
5
return 0;
6
}
Code
💡 Hint: C does not have **; for powers #include <math.h> + pow(2, 10).

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of 23 divided by 4 using %d.

🎯 Expected output: 3

Loading editor…

About this topic

C's operator set is very close to the hardware it is written for. Alongside arithmetic operators, bitwise operators are also part of the core language because C's initial use case was reading and writing hardware registers.

In C, assignment is not a statement but an expression: it produces a value. This is why confusing a single equals sign with a double equals sign does not cause a compilation error. When you accidentally write an assignment inside a condition, the code compiles and silently works incorrectly.

The increment operator has two forms, prefix and postfix, and using them more than once in the same expression produces undefined behavior. In C, the result of such expressions varies depending on the compiler.

What you will learn

  • Arithmetic, comparison, and logical operators
  • How the modulo operator behaves with negative numbers
  • Why short-circuit evaluation is important
  • The difference between the two forms of the increment operator

When to use?

Short-circuit evaluation is used not only for speed but for safety: if the left condition fails, the right one never runs. In pointer checking, this pattern is a lifesaver.

Bitwise operators are used in flag sets: storing multiple true-false values in a single integer saves both memory and time.

Common mistakes

Wrong

#include <stdio.h>

int main() {
    int age = 10;
    if (age = 18) {
        printf("Adult\n");
    }
    return 0;
}

A single equals sign is assignment, and a double equals sign is comparison. Here, 18 was assigned to the age variable and the value of the expression became 18; since 18 is not zero, the condition is always true. The program compiles without error, but it works incorrectly and the age value gets corrupted.

Correct

#include <stdio.h>

int main() {
    int age = 10;
    if (age == 18) {
        printf("Adult\n");
    }
    return 0;
}

Wrong

#include <stdio.h>

int main() {
    int i = 5;
    int result = i++ + i++;
    printf("%d\n", result);
    return 0;
}

Because the same variable is modified twice within an expression, this is undefined behavior. The result varies depending on the compiler and optimization level; the same code may print different numbers on different machines. Split increment operations into separate lines.

Correct

#include <stdio.h>

int main() {
    int i = 5;
    int result = i + (i + 1);
    i += 2;
    printf("%d %d\n", result, i);
    return 0;
}

Wrong

#include <stdio.h>

int main() {
    int a = 5, b = 10;
    if (a > 0 & b > 0) {
        printf("Both are positive\n");
    }
    return 0;
}

A single ampersand works bitwise, and for logical and, you need two. In this example, the result happens to be correct by coincidence, but short-circuit evaluation is lost: the right-hand condition is always evaluated. In pointer checking, this difference crashes the program.

Correct

#include <stdio.h>

int main() {
    int a = 5, b = 10;
    if (a > 0 && b > 0) {
        printf("Both are positive\n");
    }
    return 0;
}

Frequently asked questions

What is the result of a modulo operation with a negative number?

Since C99, the sign of the result is the same as the sign of the left operand, meaning minus seven modulo three results in minus one. In Python, the same operation gives two. If you are calculating array indices in a loop, this difference leads to negative indices and memory errors.

What is the difference between pre-increment and post-increment?

The prefix form increments the variable and produces the NEW value; the postfix form produces the OLD value, and the increment happens afterwards. On a single line by themselves, both are the same. The difference shows up when used inside an assignment, which is why using them alone is the most readable way.

Do I need to memorize operator precedence?

No. Knowing that multiplication and division come before addition and subtraction in arithmetic is enough; use parentheses for the rest. Parentheses do not slow down the code and prevent the reader from having to look up a precedence table. The precedence of bitwise operators is especially confusing, where parentheses are a must.

Summary

Assignment is an expression, so when written inside a condition, it does not produce a compilation error.

Logical and and bitwise and are different operators.

Modifying the same variable twice in an expression is undefined behavior.

Use parentheses instead of memorizing precedence.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code