how to do add to an integrr in c
To add two integers in C, you can use the addition operator (+). Here's an example with explanations for each step:
#include <stdio.h>
int main() {
int num1 = 5; // First integer
int num2 = 3; // Second integer
int sum; // Variable to store the sum
// Add the two integers
sum = num1 + num2;
// Print the result
printf("The sum is: %d\n", sum);
return 0;
}
Explanation:
1. We include the <stdio.h>
header file to use the printf
function for printing the result.
2. In the main
function, we declare two integer variables num1
and num2
and assign them the values 5 and 3, respectively.
3. We declare another integer variable sum
to store the sum of num1
and num2
.
4. Using the addition operator +
, we add num1
and num2
and assign the result to sum
.
5. We use the printf
function to print the value of sum
as the output.
The output of this program will be:
The sum is: 8
Note: This is a basic example of adding two integers in C. You can modify the values of num1
and num2
to perform addition with different numbers.