Chapter 08Beginner

C Operators and Expressions

Learn all the operators in C: math (+, -, *, /), comparisons (==, <, >), and logical operations (&&, ||). Build expressions that compute values.

15 min readUpdated 2024-12-16
operatorsarithmeticrelationallogicalassignment++--%

What You Will Learn

  • โœ“Use arithmetic operators (+, -, *, /, %)
  • โœ“Compare values with relational operators
  • โœ“Combine conditions with && (AND) and || (OR)
  • โœ“Increment/decrement with ++ and --

01What are Operators?

Operators are symbols that tell the compiler to perform specific operations on values or variables. The values they work on are called operands.

Expression: 10 + 20

10

Operand

+

Operator

20

Operand

=
30

Result

Types Based on Number of Operands

Unary (1 operand)

++a, --b, !flag

Binary (2 operands)

a + b, x * y

Ternary (3 operands)

a ? b : c

02Arithmetic Operators

Used for mathematical calculations like addition, subtraction, multiplication, and division.

Key Points for Beginners

  • โ€ข Division (/) with integers: Result is always an integer (truncated). 10 / 3 = 3, not 3.33
  • โ€ข Modulus (%): Returns the remainder after division. 10 % 3 = 1 because 10 = 3ร—3 + 1
  • โ€ข ++a vs a++: ++a increments before using the value; a++ uses the value then increments
OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33 (integer)
%Modulus (remainder)10 % 31
++Incrementa++ or ++aa + 1
--Decrementa-- or --aa - 1

๐Ÿ“ This program demonstrates all arithmetic operators with two numbers and shows the difference between pre/post increment.

arithmetic_ops.c
C
1#include <stdio.h>
2
3int main() {
4 int a = 17, b = 5;
5
6 printf("a = %d, b = %d\n\n", a, b);
7 printf("a + b = %d\n", a + b); // 22
8 printf("a - b = %d\n", a - b); // 12
9 printf("a * b = %d\n", a * b); // 85
10 printf("a / b = %d\n", a / b); // 3 (integer division)
11 printf("a %% b = %d\n", a % b); // 2 (remainder)
12
13 // Pre vs Post increment
14 int x = 5;
15 printf("\nx = %d\n", x);
16 printf("x++ = %d\n", x++); // Prints 5, then x becomes 6
17 printf("Now x = %d\n", x); // x is now 6
18 printf("++x = %d\n", ++x); // x becomes 7, then prints 7
19
20 return 0;
21}
Output

a = 17, b = 5


a + b = 22

a - b = 12

a * b = 85

a / b = 3

a % b = 2


x = 5

x++ = 5

Now x = 6

++x = 7

Pre vs Post Increment

++x (prefix): Increment first, then use the value
x++ (postfix): Use the value first, then increment

03Relational Operators

Used to compare two values. Returns 1 (true) or 0 (false).

OperatorMeaningExample (a=10, b=5)Result
==Equal toa == b0 (false)
!=Not equal toa != b1 (true)
>Greater thana > b1 (true)
<Less thana < b0 (false)
>=Greater or equala >= b1 (true)
<=Less or equala <= b0 (false)

๐Ÿ“ This program compares two numbers and prints 1 for true, 0 for false.

relational_ops.c
C
1#include <stdio.h>
2
3int main() {
4 int a = 10, b = 5;
5
6 printf("a = %d, b = %d\n\n", a, b);
7 printf("a == b: %d\n", a == b); // 0 (false)
8 printf("a != b: %d\n", a != b); // 1 (true)
9 printf("a > b: %d\n", a > b); // 1 (true)
10 printf("a < b: %d\n", a < b); // 0 (false)
11 printf("a >= b: %d\n", a >= b); // 1 (true)
12 printf("a <= b: %d\n", a <= b); // 0 (false)
13
14 return 0;
15}

04Logical Operators

Used to combine or negate conditions. Essential for complex if statements.

OperatorNameDescriptionExample
&&ANDTrue if both are true1 && 1 = 1, 1 && 0 = 0
||ORTrue if any is true1 || 0 = 1, 0 || 0 = 0
!NOTReverses the value!1 = 0, !0 = 1

๐Ÿ“ This program shows how logical operators combine conditions and how NOT reverses boolean values.

logical_ops.c
C
1#include <stdio.h>
2
3int main() {
4 int a = 1, b = 0; // 1 = true, 0 = false
5
6 printf("a = %d (true), b = %d (false)\n\n", a, b);
7
8 // AND: both must be true
9 printf("a && b = %d\n", a && b); // 0 (false)
10 printf("a && a = %d\n", a && a); // 1 (true)
11
12 // OR: at least one must be true
13 printf("a || b = %d\n", a || b); // 1 (true)
14 printf("b || b = %d\n", b || b); // 0 (false)
15
16 // NOT: reverse the value
17 printf("!a = %d\n", !a); // 0 (false)
18 printf("!b = %d\n", !b); // 1 (true)
19
20 // Practical example
21 int age = 25;
22 int hasLicense = 1;
23 if (age >= 18 && hasLicense) {
24 printf("\nYou can drive!\n");
25 }
26
27 return 0;
28}

05Bitwise Operators

Operate on individual bits of integers. Used in low-level programming, flags, and optimization.

OpNameDescription
&AND1 if both bits are 1
|OR1 if any bit is 1
^XOR1 if bits are different
~NOTFlip all bits
<<Left ShiftMultiply by 2โฟ
>>Right ShiftDivide by 2โฟ

Visual: 5 & 3 (Bitwise AND)

5 =0101
3 =0011
5 & 3 =0001= 1

๐Ÿ“ This program demonstrates bitwise operations and shows how left/right shift multiplies or divides by powers of 2.

bitwise_ops.c
C
1#include <stdio.h>
2
3int main() {
4 int a = 5, b = 3; // 5 = 0101, 3 = 0011
5
6 printf("a = %d (0101), b = %d (0011)\n\n", a, b);
7
8 printf("a & b = %d\n", a & b); // 1 (0001)
9 printf("a | b = %d\n", a | b); // 7 (0111)
10 printf("a ^ b = %d\n", a ^ b); // 6 (0110)
11 printf("~a = %d\n", ~a); // -6 (inverts all bits)
12
13 // Shift operators
14 printf("\na << 1 = %d\n", a << 1); // 10 (multiply by 2)
15 printf("a << 2 = %d\n", a << 2); // 20 (multiply by 4)
16 printf("a >> 1 = %d\n", a >> 1); // 2 (divide by 2)
17
18 return 0;
19}

๐Ÿ’ก Shift Trick

x << n = x ร— 2โฟ (fast multiplication)
x >> n = x รท 2โฟ (fast division)

06Assignment Operators

Used to assign values to variables. Compound operators combine assignment with another operation.

OperatorEquivalent
a = ba = b
a += ba = a + b
a -= ba = a - b
a *= ba = a * b
a /= ba = a / b
a %= ba = a % b
OperatorEquivalent
a &= ba = a & b
a |= ba = a | b
a ^= ba = a ^ b
a <<= ba = a << b
a >>= ba = a >> b

๐Ÿ“ This program shows compound assignment operators in action โ€” they modify the variable in place.

assignment_ops.c
C
1#include <stdio.h>
2
3int main() {
4 int x = 10;
5 printf("Initial x = %d\n\n", x);
6
7 x += 5; printf("x += 5 โ†’ x = %d\n", x); // 15
8 x -= 3; printf("x -= 3 โ†’ x = %d\n", x); // 12
9 x *= 2; printf("x *= 2 โ†’ x = %d\n", x); // 24
10 x /= 4; printf("x /= 4 โ†’ x = %d\n", x); // 6
11 x %= 4; printf("x %%= 4 โ†’ x = %d\n", x); // 2
12
13 return 0;
14}

07Other Operators

๐Ÿ“ sizeof Operator - Getting Size in Bytes

sizeof is a compile-time operator that returns the size (in bytes) of a variable or data type. It's not a function - no function call overhead!

size_t sizeof(type_or_variable)
Returns: Size in bytes (type: size_t, use %zu to print)
Usage: sizeof(int), sizeof(arr), sizeof(x)
OperatorDescriptionExample
sizeof()Returns size in bytes (compile-time)sizeof(int) โ†’ 4
? :Ternary (shorthand if-else)a > b ? a : b
&Address-of operator&x โ†’ 0x7fff...
*Dereference (get value at address)*ptr โ†’ value
(type)Type cast (convert to type)(float)5 โ†’ 5.0
,Comma (evaluates left-to-right)(a=1, b=2) โ†’ 2

๐Ÿ“ This program demonstrates sizeof, ternary operator, address-of, and type casting.

other_ops.c
C
1#include <stdio.h>
2
3int main() {
4 // sizeof - get size in bytes
5 printf("sizeof(int): %zu bytes\n", sizeof(int));
6 printf("sizeof(char): %zu byte\n", sizeof(char));
7
8 // Ternary operator (condition ? if_true : if_false)
9 int a = 10, b = 20;
10 int max = (a > b) ? a : b;
11 printf("\nMax of %d and %d is %d\n", a, b, max);
12
13 // Address-of operator
14 int x = 42;
15 printf("\nValue of x: %d\n", x);
16 printf("Address of x: %p\n", (void*)&x);
17
18 // Type casting
19 int num = 7;
20 printf("\n7 / 2 = %d (int)\n", num / 2);
21 printf("7 / 2 = %.2f (float)\n", (float)num / 2);
22
23 return 0;
24}

08C Expressions

An expression is a combination of operands (values/variables) and operators that evaluates to a single value. Expressions are the building blocks of C statements.

Expression TypeDescriptionExample
ConstantContains only constant values5 + 3 * 2
IntegralProduces integer resulta + b * c
FloatingProduces float/double result3.14 * r * r
RelationalProduces true (1) or false (0)a > b
LogicalCombines conditionsa > 0 && b > 0
PointerProduces memory address&x, ptr + 1

๐Ÿ“ This program demonstrates different types of expressions and how they evaluate to single values.

expressions.c
C
1#include <stdio.h>
2
3int main() {
4 int a = 10, b = 5;
5 float pi = 3.14, r = 2.0;
6
7 // Constant expression (evaluated at compile time)
8 int size = 5 + 3 * 2; // 11
9 printf("Constant: 5 + 3 * 2 = %d\n", size);
10
11 // Arithmetic expression
12 int sum = a + b * 2; // 10 + 10 = 20
13 printf("Arithmetic: a + b * 2 = %d\n", sum);
14
15 // Floating expression
16 float area = pi * r * r; // 3.14 * 4 = 12.56
17 printf("Floating: pi * r * r = %.2f\n", area);
18
19 // Relational expression (returns 0 or 1)
20 int isGreater = a > b; // 1 (true)
21 printf("Relational: a > b = %d\n", isGreater);
22
23 // Logical expression
24 int bothPositive = (a > 0) && (b > 0); // 1 (true)
25 printf("Logical: (a > 0) && (b > 0) = %d\n", bothPositive);
26
27 // Assignment expression (returns assigned value)
28 int x, y;
29 x = (y = 5) + 3; // y=5, x=8
30 printf("Assignment: x = (y = 5) + 3 โ†’ x=%d, y=%d\n", x, y);
31
32 // Comma expression (evaluates left-to-right, returns last)
33 int result = (a = 1, b = 2, a + b); // result = 3
34 printf("Comma: (a=1, b=2, a+b) = %d\n", result);
35
36 return 0;
37}
Output

Constant: 5 + 3 * 2 = 11

Arithmetic: a + b * 2 = 20

Floating: pi * r * r = 12.56

Relational: a > b = 1

Logical: (a > 0) && (b > 0) = 1

Assignment: x = (y = 5) + 3 โ†’ x=8, y=5

Comma: (a=1, b=2, a+b) = 3

๐Ÿ’ก Expression vs Statement

Expression: a + b โ€” Produces a value
Statement: x = a + b; โ€” Performs an action (ends with ;)

09Operator Precedence

When an expression has multiple operators, precedence determines which operator is evaluated first. This is why 10 + 20 * 30 equals 610, not 900!

Quick Example

10+20 * 30

* is evaluated before + (higher precedence)

= 10 + 600 = 610

๐Ÿ“š Full Tutorial Available

Learn the complete precedence table, associativity rules, and the PUMAS REBL TAC memory trick.

Learn Precedenceโ†’

10Common Mistakes

โŒ Using = instead of ==

main.c
C
if (a = 5) { } // WRONG! This assigns 5 to a
if (a == 5) { } // Correct! This compares a to 5

โŒ Integer Division Confusion

main.c
C
int result = 5 / 2; // Result: 2, not 2.5!
float result = 5.0 / 2; // Result: 2.5
float result = (float)5 / 2; // Result: 2.5

โŒ Confusing & with &&

main.c
C
if (a & b) { } // Bitwise AND (rarely intended)
if (a && b) { } // Logical AND (usually intended)

11Summary

What You Learned:

  • โœ“Arithmetic: +, -, *, /, %, ++, --
  • โœ“Relational: ==, !=, >, <, >=, <=
  • โœ“Logical: &&, ||, !
  • โœ“Bitwise: &, |, ^, ~, <<, >>
  • โœ“Assignment: =, +=, -=, *=, /=, etc.
  • โœ“Expressions: Combinations of operands and operators

12Next Steps

Continue your learning journey: