C Programming
Maximum of Three Numbers
Conditional Statements
Ternary Operator
Programming Techniques

Find maximum of three number in C without using conditional statement and ternary operator

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If the goal is to find the maximum of three numbers in C without if or the ternary operator, the cleanest trick is usually to build a branch-free max formula for two numbers and apply it twice. This is mostly an interview-style constraint, not something you would normally optimize for in production code.

A Math-Based Max Formula

For two integers, one common formula is:

text
max(a, b) = (a + b + abs(a - b)) / 2

That works because:

  • If a >= b, then abs(a - b) becomes a - b
  • If b > a, then abs(a - b) becomes b - a

In C:

c
1#include <stdio.h>
2#include <stdlib.h>
3
4int max2(int a, int b) {
5    return (a + b + abs(a - b)) / 2;
6}
7
8int main(void) {
9    int a = 7, b = 15, c = 11;
10    int max = max2(max2(a, b), c);
11    printf("%d\n", max);
12    return 0;
13}

This avoids both conditionals and the ternary operator.

Extend It to Three Numbers

Once you can compute the maximum of two numbers, the maximum of three is just:

text
max(a, b, c) = max(max(a, b), c)

So the problem reduces naturally:

c
int max3(int a, int b, int c) {
    return max2(max2(a, b), c);
}

That keeps the code short and readable.

A Bitwise Alternative

Another class of solutions uses bit operations to avoid explicit branches:

c
int max2_bitwise(int a, int b) {
    return a - ((a - b) & ((a - b) >> 31));
}

Then:

c
int max3_bitwise(int a, int b, int c) {
    return max2_bitwise(max2_bitwise(a, b), c);
}

This style is more "clever," but it is also less portable and easier to misunderstand because it depends on signed shift behavior and integer representation assumptions.

Why the Math Version Is Usually Better

For explanation purposes, the arithmetic formula is easier to justify than the bitwise trick. It makes the intent obvious and does not rely on reading sign bits manually.

However, it still has a caveat: the expression a + b or a - b can overflow for very large integers. In interview-sized examples that may be ignored, but in real systems it matters.

That overflow risk is one reason these branch-free formulas are usually treated as exercises rather than as broadly safe utility functions.

This Is Mostly a Constraint Exercise

In real C code, you would almost always write:

c
int max = a;
if (b > max) max = b;
if (c > max) max = c;

That version is clearer, easier to maintain, and less error-prone. The branch-free versions are mainly useful as a puzzle or to understand how arithmetic and bit tricks can encode comparisons.

Common Pitfalls

  • Forgetting that abs(a - b) can overflow for edge integer values.
  • Using signed bit-shift tricks without understanding portability implications.
  • Solving the interview constraint and then treating the trick as generally better production code.
  • Assuming branch-free code is automatically faster in every real compiler and CPU scenario.

Summary

  • You can solve the problem without if or ?: by computing max for two values and reusing it.
  • The arithmetic formula with abs is the most readable trick.
  • Bitwise approaches exist but are harder to justify and maintain.
  • These solutions are mostly puzzle-style exercises.
  • In real code, clarity usually beats branch-avoidance tricks.

Course illustration
Course illustration

All Rights Reserved.