C Program to Add Two Numbers
C Program to Add Two Numbers
A school exercise asks you to add two numbers. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Add two integer values with + after choosing a type that can represent their total.
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 add two numbers uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Change the marks to 45 and 38.
Add Two Numbers: follow the data
Given data
A school exercise asks you to add two numbers. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Rule
Add two integer values with + after choosing a type that can represent their total.
Observed result
Total: 75
Understand the operation
A student total combines both subject marks. The addition produces a new value; neither input variable must be changed to print the sum.
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 maths = 40;
int science = 35;
printf("Total: %d\n", maths + science);
return 0;
}
Expected output
Total: 75
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int maths = 40;
int science = 35;
printf("Total: %d\n", maths + science);
return 0;
}
maths is 40 and science is 35.
The expression 40 + 35 produces 75, which %d formats as a whole number.
Follow the worked calculation
Start with the sample
maths is 40 and science is 35.
Apply the rule
The expression 40 + 35 produces 75, which %d formats as a whole number.
Check the result
Total: 75
Total: 75
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int maths = 40;
int science = 35;
printf("Total: %d\n", maths + science);
return 0;
}
Change the marks to 45 and 38.
Worked answer
The worked answer follows the same rule with the requested changed data. Compare its complete source and output: Total: 83. The result should follow from the operations, rather than from memorizing the first sample.
C17 · complete program
#include <stdio.h>
int main(void) {
int maths = 45;
int science = 38;
printf("Total: %d\n", maths + science);
return 0;
}
Expected output
Total: 83
Run in C compiler →Recognize the important boundaries
Adding two valid integers can overflow their type. Check the resulting range, not only each individual input.
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
Add two integer values with + after choosing a type that can represent their total.
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.