If you are learning C programming, you might have encountered some problems that require you to perform some basic arithmetic operations, such as addition, subtraction, multiplication and division. For example, how do you add two integers in C? How do you store and display the result?
In this program, I will show you how to write a simple C program that can accept two integers from the user and calculate the sum of the two integers. You will also learn some basic concepts of C programming, such as variables, data types, operators, input/output and comments.
C program
- To add two integers in C, we need to follow these steps:
- Declare three integer variables: 'a', 'b' and 'sum'.
- Ask the user to enter the first integer and store it in the variable 'a'.
- Ask the user to enter the second integer and store it in the variable 'b'.
- Add the two integers using the '+' operator and store the result in the variable 'sum'.
- Print the result using the 'printf' function.
Now that we know the steps to add two integers in C, let's write the C program using these steps. Here is the complete code:
#include <stdio.h>
int main()
{
int a, b, sum;
printf("Enter the first integer: ");
scanf("%d", &a);
printf("Enter the second integer: ");
scanf("%d", &b);
sum = a + b; // calculate the sum of the two integers
printf("The sum of %d and %d is %d\n", a, b, sum);
return 0;
}
Explanation
Let's go through each line of code and understand what it does.
- The first line '#include <stdio.h>' is a preprocessor directive that tells the compiler to include the standard input/output header file. This file contains some functions that we will use later in our program, such as 'printf' and 'scanf'.
- The second line 'int main()' is the main function of our program. Every C program must have a main function where the execution starts.
- The third line '{' marks the beginning of the main function body.
- The fourth line 'int a, b, sum;' declares three integer variables: 'a', 'b' and 'sum'. An integer variable can store a whole number, such as 1, 2 or 1000. We use these variables to store the input and output values of our program.
- The fifth line 'printf("Enter the first integer: ");' uses the 'printf' function to print a message on the screen. The message is enclosed in double quotes and ends with a semicolon. The message ask the user to enter the first integer.
- The sixth line 'scanf("%d", &a);' uses the 'scanf' function to read an integer value from the user and store it in the variable 'a'. The '%d' is a format specifier that tells the function what type of value to expect. The '&' is an operator that gives the address of a variable. The function needs this address to store the value in that variable. The semicolon marks the end of a statement.
- The seventh line 'printf("Enter the second integer: ");' uses the 'printf' function to print another message on the screen. The message asks the user to enter the second integer.
- The eighth line 'scanf("%d", &b);' uses the 'scanf' function to read another integer value from the user and store it in the variable 'b'.
- The ninth line 'sum = a + b;' calculates the sum of the two integers using the '+' operator and stores the result in the variable 'sum'. The '=' is an operator that assigns a value to a variable.
- The tenth line 'printf("The sum of %d and %d is %d\n", a, b, sum);' uses the 'printf' function to print the result on the screen. The function takes a format string and a list of values as arguments. The format string contains some placeholders that are replaced by the values in the list. For example, the first '%d' is replaced by the value of 'a', the second '%d' is replaced by the value of 'b', and so on. The '\n' is a special character that represents a new line. It moves the cursor to the next line after printing.
- The eleventh line 'return 0;' ends the main function and returns a value of 0 to indicate that the program ran successfully.
- The twelfth line '}' marks the end of the main function body.
Output
Here are some sample inputs and outputs of
Enter the first integer: 10
Enter the second integer: 20
The sum of 10 and 20 is 30
Enter the first integer: -5
Enter the second integer: 7
The sum of -5 and 7 is 2
Enter the first integer: 0
Enter the second integer: 0
The sum of 0 and 0 is 0
Conclusion
In this program, you learned how to write a C program that can accept two integers from the user and calculate their sum. You also learned some basic concepts of C programming, such as variables, data types, operators, input/output and comments.
I hope you found this blog post useful and interesting. If you have any questions or feedback, please leave a comment below. Thank you for reading!
We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!