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:

  1. We include the <stdio.h> header file, which allows us to use the printf and scanf functions for input and output operations.

  2. We define a main function, which is the entry point of the program.

  3. Inside the main function, we declare three variables: num1, num2, and sum. These variables will hold the user input and the result of the addition.

  4. We use the printf function to display a message asking the user to enter the first number.

  5. We use the scanf function to read the user input and store it in the num1 variable. The %d format specifier is used to read an integer value.

  6. We use the printf function again to prompt the user to enter the second number.

  7. We use the scanf function again to read the user input and store it in the num2 variable.

  8. We perform the addition operation by using the "+" operator and assign the result to the sum variable.

  9. We use the printf function once more to display the result to the user. The format specifier %d is used to print the values of num1, num2, and sum.

  10. 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.