C Program to Swap Two Numbers
C Program to Swap Two Numbers
A school exercise asks you to swap two values. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Swap two stored values by saving one before overwriting its original location.
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 swap two values uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Swap 45 and 38 using the same three stores.
Swap Two Values: follow the data
Given data
A school exercise asks you to swap two values. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Rule
Swap two stored values by saving one before overwriting its original location.
Observed result
First: 35; second: 40
Understand the operation
A temporary object preserves the first mark while the first variable takes the second mark. Then the preserved mark can move into the second location.
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 first = 40, second = 35;
int saved = first;
first = second;
second = saved;
printf("First: %d; second: %d\n", first, second);
return 0;
}
Expected output
First: 35; second: 40
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int first = 40, second = 35;
int saved = first;
first = second;
second = saved;
printf("First: %d; second: %d\n", first, second);
return 0;
}
saved receives 40 before first changes.
first becomes 35; second then receives saved, producing 40.
Follow the worked calculation
Start with the sample
saved receives 40 before first changes.
Apply the rule
first becomes 35; second then receives saved, producing 40.
Check the result
First: 35; second: 40
First: 35; second: 40
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int first = 40, second = 35;
int saved = first;
first = second;
second = saved;
printf("First: %d; second: %d\n", first, second);
return 0;
}
Swap 45 and 38 using the same three stores.
Worked answer
The worked answer follows the same rule with the requested changed data. Compare its complete source and output: First: 38; second: 45. The result should follow from the operations, rather than from memorizing the first sample.
C17 · complete program
#include <stdio.h>
int main(void) {
int first = 45, second = 38;
int saved = first;
first = second;
second = saved;
printf("First: %d; second: %d\n", first, second);
return 0;
}
Expected output
First: 38; second: 45
Run in C compiler →Recognize the important boundaries
Arithmetic swaps can overflow and XOR swaps have aliasing and readability pitfalls. A temporary is the straightforward choice.
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
Swap two stored values by saving one before overwriting its original location.
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.