C Program to Find the Largest of Three Numbers
C Program to Find the Largest of Three Numbers
A school exercise asks you to largest of three. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Keep the largest value seen so far, starting from an actual input rather than an invented zero.
Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.
Explain
Explain why largest of three uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Find the largest of -5, -2 and -7.
Largest of Three: follow the data
Given data
A school exercise asks you to largest of three. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Rule
Keep the largest value seen so far, starting from an actual input rather than an invented zero.
Observed result
Largest: 45
Understand the operation
After checking each candidate, the stored largest is at least every value already examined. This invariant explains why the final value is correct.
Read the declarations and operands before the output call. %d formats an int; floating-point results use an appropriate precision with %f. A format describes a value that was already computed—it does not choose the earlier arithmetic.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int a = 40, b = 45, c = 38;
int largest = a;
if (b > largest) largest = b;
if (c > largest) largest = c;
printf("Largest: %d\n", largest);
return 0;
}
Expected output
Largest: 45
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int a = 40, b = 45, c = 38;
int largest = a;
if (b > largest) largest = b;
if (c > largest) largest = c;
printf("Largest: %d\n", largest);
return 0;
}
Start at 40; checking 45 replaces it.
38 does not exceed 45, so the answer remains 45.
Follow the worked calculation
Start with the sample
Start at 40; checking 45 replaces it.
Apply the rule
38 does not exceed 45, so the answer remains 45.
Check the result
Largest: 45
Largest: 45
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int a = 40, b = 45, c = 38;
int largest = a;
if (b > largest) largest = b;
if (c > largest) largest = c;
printf("Largest: %d\n", largest);
return 0;
}
Find the largest of -5, -2 and -7.
Worked answer
The worked answer follows the same rule with the requested changed data. Compare its complete source and output: Largest: -2. The result should follow from the operations, rather than from memorizing the first sample.
C17 · complete program
#include <stdio.h>
int main(void) {
int a = -5, b = -2, c = -7;
int largest = a;
if (b > largest) largest = b;
if (c > largest) largest = c;
printf("Largest: %d\n", largest);
return 0;
}
Expected output
Largest: -2
Run in C compiler →Recognize the important boundaries
Initializing to zero would fail when all supplied values are negative. Equality does not require a replacement when only the value matters.
The demonstrated values stay within the types’ representable ranges. A program accepting arbitrary external values needs checked conversion and bounds before performing arithmetic.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Keep the largest value seen so far, starting from an actual input rather than an invented zero.
Continue with arithmetic operand types.
Continue with validated input.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.