Overview: If you are learning how to code in C, one of the most important concepts you will need to understand is how to use operators. Operators are the building blocks of logic and math in your code. This easy-to-understand guide will show you exactly what operators are, how they work, and how to use them correctly.
1. What is an Operator?
An Operator is simply a symbol that tells the computer to perform a specific math or logic task. An Operand is the data (the numbers or variables) that the operator works on.
Simple Example:
5 + 3
In this example, the + symbol is the
operator, and the numbers 5 and
3 are the operands.
2. Arithmetic Operators (Basic Math)
Arithmetic operators are used to perform everyday math calculations like addition and multiplication.
| Operator | What it Does |
|---|---|
| + | Addition (Adds two values) |
| - | Subtraction (Subtracts the second value from the first) |
| * | Multiplication (Multiplies two values) |
| / | Division (Divides the first value by the second) |
| % | Modulus (Divides two values and returns the remainder) |
Important Note: The Modulus operator (%)
only works with whole numbers (integers). You cannot use it with decimal
numbers (floats).
3. Relational Operators (Comparing Values)
Relational operators are used to compare two values. They ask a question (like "Is 5 greater than 3?") and return an answer of either True (1) or False (0).
| Operator | What it Does |
|---|---|
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
| == | Equal to (Checks if two values are exactly the same) |
| != | Not equal to |
4. Logical Operators (AND, OR, NOT)
Logical operators are used when you want to combine multiple conditions together. For example, checking if a user is "logged in" AND "over 18 years old".
| Operator | What it Does |
|---|---|
| && | Logical AND: True only if BOTH conditions are true. |
| || | Logical OR: True if AT LEAST ONE condition is true. |
| ! | Logical NOT: Reverses the result (turns True to False). |
5. Bitwise Operators (Advanced Binary)
Bitwise operators look at data at its most basic level: 1s and 0s (binary). You probably won't use these much as a beginner, but they are very important for advanced programming, hardware control, and security.
| Operator | What it Does |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| << | Left-shift |
| >> | Right-shift |
| ~ | Bitwise Complement (Inverts 1s to 0s, and 0s to 1s) |
6. Assignment and Conditional Operators
The Assignment Operator (=) is used to store
a value inside a variable. Note: A single = assigns a value,
while a double == compares two values.
The Conditional Operator (?:) is a quick,
one-line shortcut for writing an if-else statement.
condition ? do_this_if_true : do_this_if_false;
7. The sizeof() Operator (Checking Memory Space)
The sizeof operator is a special tool that tells you exactly
how much computer memory (in bytes) a specific data type or variable is
using.
Note: Many older textbooks say that an integer (int) takes 2 bytes. On modern computers and operating systems, an integer almost always takes 4 bytes.
| Syntax | Result (Memory Space) |
|---|---|
| sizeof(char); | 1 byte |
| sizeof(int); | 4 bytes |
| sizeof(float); | 4 bytes |
8. Increment (++) and Decrement (--) Operators
These are handy shortcuts for adding or subtracting the number 1 from a variable.
-
Increment (
++): Adds exactly 1 to the variable. -
Decrement (
--): Subtracts exactly 1 from the variable.
Pre vs. Post (Before vs. After)
Placing the plus or minus signs before or after the variable name changes how the computer reads the code, especially if it is part of a larger math equation.
| Format | How it Works |
|---|---|
Pre-Increment (++x) |
Add first, then use. The computer adds 1 to the variable immediately, and then uses the new updated number in the rest of the equation. |
Post-Increment (x++) |
Use first, then add. The computer uses the original, older number in the equation first. Once the equation is done, it adds 1 to the variable in the background. |
Write a Comment