Make sure you are using an appropriate -march setting, first off. GCC defaults to not using any instructions that were not supported on the original i386 - allowing it to use newer instruction sets can make a BIG difference at times! On -march=core2 -O2 I get:

min:
    pushl   %ebp
    movl    %esp, %ebp
    movl    8(%ebp), %edx
    movl    12(%ebp), %ecx
    movl    16(%ebp), %eax
    cmpl    %edx, %ecx
    leave
    cmovbe  %ecx, %edx
    cmpl    %eax, %edx
    cmovbe  %edx, %eax
    ret

The use of cmov here may help you avoid branch delays - and you get it without any inline asm just by passing in -march. When inlined into a larger function this is likely to be even more efficient, possibly just four assembly operations. If you need something faster than this, see if you can get the SSE vector operations to work in the context of your overall algorithm.

Answer from bdonlan on Stack Overflow
Top answer
1 of 10
11

Make sure you are using an appropriate -march setting, first off. GCC defaults to not using any instructions that were not supported on the original i386 - allowing it to use newer instruction sets can make a BIG difference at times! On -march=core2 -O2 I get:

min:
    pushl   %ebp
    movl    %esp, %ebp
    movl    8(%ebp), %edx
    movl    12(%ebp), %ecx
    movl    16(%ebp), %eax
    cmpl    %edx, %ecx
    leave
    cmovbe  %ecx, %edx
    cmpl    %eax, %edx
    cmovbe  %edx, %eax
    ret

The use of cmov here may help you avoid branch delays - and you get it without any inline asm just by passing in -march. When inlined into a larger function this is likely to be even more efficient, possibly just four assembly operations. If you need something faster than this, see if you can get the SSE vector operations to work in the context of your overall algorithm.

2 of 10
11

Assuming your compiler isn't out to lunch, this should compile down to two compares and two conditional moves. It isn't possible to do much better than that.

If you post the assembly that your compiler is actually generating, we can see if there's anything unnecessary that's slowing it down.

The number one thing to check is that the routine is actually getting inlined. The compiler isn't obligated to do so, and if it's generating a function call, that will be hugely expensive for such a simple operation.

If the call really is getting inlined, then loop unrolling may be beneficial, as DigitalRoss said, or vectorization may be possible.

Edit: If you want to vectorize the code, and are using a recent x86 processor, you will want to use the SSE4.1 pminud instruction (intrinsic: _mm_min_epu32), which takes two vectors of four unsigned ints each, and produces a vector of four unsigned ints. Each element of the result is the minimum of the corresponding elements in the two inputs.

I also note that your compiler used branches instead of conditional moves; you should probably try a version that uses conditional moves first and see if that gets you any speedup before you go off to the races on a vector implementation.

🌐
Wikiversity
en.wikiversity.org › wiki › C_Source_Code › Find_minimum_of_three_numbers_using_ternary_(conditional)_operator
C Source Code/Find minimum of three numbers using ternary (conditional) operator - Wikiversity
April 19, 2020 - /* To find the minimum of three numbers using the conditional operator */ #include <stdio.h> int main(void) { int a, b, c, temp, min; printf ("Enter three nos. separated by spaces: "); scanf ("%d%d%d", &a, &b, &c); temp = (a < b) ? a : b; min = (c < temp) ? c : temp; printf ("The Minimum of the three is: %d", min); /* indicate success */ return 0; } /* Output: Enter three nos.
Top answer
1 of 10
105

If possible, I recommend using C++11 or newer which allows you to compute the desired result w/out implementing your own function (std::min). As already pointed out in one of the comments, you can do

T minimum(std::min({x, y, z}));

or

T minimum = std::min({x, y, z});

which stores the minimum of the variables x, y and z in the variable minimum of type T (note that x, y and z must have the same type or have to be implicitly convertible to it). Correspondingly, the same can be done to obtain a maximum: std::max({x, y, z}).

2 of 10
51

There's a number of improvements that can be made.

You could use standard functions to make it clearer:

// Notice I made the return type an int instead of a float, 
// since you're passing in ints
int smallest(int x, int y, int z){
    return std::min(std::min(x, y), z);
}

Or better still, as pointed out in the comments:

int smallest(int x, int y, int z){
    return std::min({x, y, z});
}

If you want it to operate on any number of ints, you could do something like this:

int smallest(const std::vector<int>& intvec){
    int smallest = std::numeric_limits<int>::max(); // Largest possible integer
    // there are a number of ways to structure this loop, this is just one
    for (int i = 0; i < intvec.size(); ++i) 
    {
        smallest = std::min(smallest, intvec[i]);
    }
    return smallest;
}

You could also make it generic so that it'll operate on any type, instead of just ints

template <typename T>
T smallest(const std::vector<T>& vec){
    T smallest = std::numeric_limits<T>::max(); // Largest possible integer
    // there are a number of ways to structure this loop, this is just one
    for (int i = 0; i < vec.size(); ++i) 
    {
        smallest = std::min(smallest, vec[i]);
    }
    return smallest;
}
🌐
Cprogrammingcode
cprogrammingcode.com › 2016 › 01 › find-smallest-of-three-numbers-in-c-c.html
Programming Tutorials: Program to Find Smallest of three Numbers in C, C++
#include <iostream> using namespace std; int main() { int a, b, c; cout << "Enter three numbers \n"; /* Taking input */ cin >> a >> b >> c; /* If a is smaller than b and c. */ if (a < b && a < c) { cout << "Smallest number is " << a; /* If b is smaller than a and c */ } else if (b < a && b < c) { cout << "Smallest number is " << b; } else { cout << "Smallest number is "<< c; } return 0; } Output : Enter three numbers : 8 1 5 Smallest number is : 1
🌐
Blogger
cprogsoutput.blogspot.com › 2012 › 04 › to-find-maximum-minimum-of-three.html
C Programs: TO FIND MAXIMUM & MINIMUM OF THREE NUMBERS
#include int main() { int a1,a2,a3,max,min; printf("enter value of a1, a2, a3\n"); scanf("%d%d%d",&a1,&a2,&a3); if(a1a3) { max= a2; } else { max = a3; } } else { min =a3; max = a2; } } else if(a2a3) { max =a1; } else max = a3; } else { min =a3; if(a2>a1) { max = a2; } else max = a1; } printf(" maximum = %d\n",max); printf(" minimum = %d\n",min); return(0); } Unknown said... ... Anonymous said... ... Try this #include int main() { int a, b, c,max,min; printf("Enter three numbers\n"); scanf("%d%d%d", &a, &b, &c); max= (a>b)&&(a>c)?a : (b>a)&&(b>c)?b : c; printf("The maximum number: %d\n", max); min = (a<b)&&(a<c)?a : (b<a)&&(b<c)?b : c; printf("The minimum number: %d\n", min); return 0; }
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › smallest-of-three-integers-without-comparison-operators
Smallest of three integers without comparison operators - GeeksforGeeks
March 12, 2025 - Input: a = 6, b = 1, c = 9 Output: 1 Explanation: The smallest of the 3 integers is b, which is equal to 1. ... The idea is to find the smallest number by repeatedly subtracting 1 from all three numbers until one of them reaches 0. The number ...
Find elsewhere
🌐
CodeScracker
codescracker.com › c › program › c-program-find-smallest-of-three-numbers.htm
C Program to Find the Smallest of Three Numbers
The program was written in the Code::Blocks IDE. Here is the first snapshot of the sample run after a successful build and run: Supply any three numbers and press the ENTER key to see the smallest number among the given three numbers, as shown here in the snapshot given below:
🌐
Codeforwin
codeforwin.org › home › c program to find maximum between three numbers
C program to find maximum between three numbers - Codeforwin
July 20, 2025 - /** * C program to find maximum between three numbers using nested if */ #include <stdio.h> int main() { int num1, num2, num3, max; /* Input three numbers from user */ printf("Enter three numbers: "); scanf("%d%d%d", &num1, &num2, &num3); if(num1 > num2) { if(num1 > num3) { /* If num1 > num2 and num1 > num3 */ max = num1; } else { /* If num1 > num2 but num1 > num3 is not true */ max = num3; } } else { if(num2 > num3) { /* If num1 is not > num2 and num2 > num3 */ max = num2; } else { /* If num1 is not > num2 and num2 > num3 */ max = num3; } } /* Print maximum value */ printf("Maximum among all three numbers = %d", max); return 0; } The above approach is lengthy and not recommended for this problem. You can combine relational and logical operator along with ladder if...else...if to find maximum in more simple way. Instead of using nested if else.
🌐
Sololearn
sololearn.com › en › Discuss › 1059436 › write-a-program-in-c-to-find-maximum-and-minimum-of-three-inputs-without-using-if-statement
Write a program in C to find maximum and minimum of three inputs without using if statement | Sololearn: Learn to code for FREE!
#include <stdio.h> int main() { int a, b, c; scanf("%d", &a); scanf("%d", &b); scanf("%d", &c); int max = (a >= b && a >= c) ? a : (b >= a && b >= c) ? b : c; int min = (a <= b && a <= c) ? a : (b <= a && b <= c) ?
🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c programs › c program to find smallest of given 3 numbers
C program to find smallest of given 3 numbers | C Program
July 18, 2020 - Example C program to find smallest of given 3 numbers:#include int main(){ float a, b, c; printf("nPlease enter 3 numbers: ");
🌐
AlgoCademy
algocademy.com › link
Minimum Value Of Three in C++ | AlgoCademy
A = 7, B = 4, C = 9 -> Output: 4 A = 3, B = 8, C = 3 -> Output: 3 A = -1, B = -2, C = -3 -> Output: -3 A = 0, B = 0, C = 0 -> Output: 0 A = 5, B = 5, C = 5 -> Output: 5 · Testing frameworks like Google Test can be used for automated testing. ... Break down the problem into smaller parts. Use simple conditional statements to compare values. Consider edge cases and test your solution against them. Practice similar problems to improve your problem-solving skills. In this blog post, we discussed how to find the minimum of three integers without using built-in functions.
🌐
YouTube
youtube.com › watch
C Program: Find Maximum and Minimum of Three Numbers - YouTube
The program first reads three integers from the user. It uses simple if conditions to compare the numbers and determine the maximum and minimum values. The r...
Published   September 1, 2024
🌐
GitHub
github.com › programworld999 › c_assignment › blob › master › Program to find maximum and minimum of 3 numbers.c
c_assignment/Program to find maximum and minimum of 3 numbers.c at master · ssadev/c_assignment
// 8) Program to find maximum and minimum of 3 numbers · int main(void) { · · int a, b, c ; · printf("Enter value of a: "); · scanf("%d", &a); · printf("Enter value of b: "); · scanf("%d", &b); · printf("Enter value of c: "); · scanf("%d", &c); ·
Author   ssadev
🌐
Computerscienceai
computerscienceai.com › 2018 › 02 › c-program-to-accept-three-integers-as.html
C Program to Find Minimum, Maximum and Average of Three Numbers
November 1, 2025 - /* Aim: To find maximum, minimum and average of three numbers */ #include<stdio.h> #include<stdlib.h> int main() { int a,b,c; printf("\n Enter 3 numbers:- "); scanf(" %d %d %d",&a,&b,&c); // Code to find maximum number if(a>b) { if(a>c) printf("\n ...