como somar em C
To perform addition in the C programming language, you can use the "+" operator. Here is an example of how to add two numbers in C:
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}
Let's break down the steps in the code:
We include the
<stdio.h>
header file, which allows us to use theprintf
andscanf
functions for input and output operations.We define a
main
function, which is the entry point of the program.Inside the
main
function, we declare three variables:num1
,num2
, andsum
. These variables will hold the user input and the result of the addition.We use the
printf
function to display a message asking the user to enter the first number.We use the
scanf
function to read the user input and store it in thenum1
variable. The%d
format specifier is used to read an integer value.We use the
printf
function again to prompt the user to enter the second number.We use the
scanf
function again to read the user input and store it in thenum2
variable.We perform the addition operation by using the "+" operator and assign the result to the
sum
variable.We use the
printf
function once more to display the result to the user. The format specifier%d
is used to print the values ofnum1
,num2
, andsum
.Finally, we return 0 from the
main
function to indicate successful execution of the program.
By following these steps, you can perform addition in C and display the result to the user.