c syntax
#include <stdio.h>
int main() {
// Declare variables
int num1, num2, sum;
// Get input from user
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Calculate the sum
sum = num1 + num2;
// Display the result
printf("Sum: %d\n", sum);
return 0;
}
Explanation:
- #include <stdio.h>
: This line includes the standard input/output library, which is necessary for using functions like printf
and scanf
.
int main() {
: This line marks the beginning of the main function, where the execution of the program starts.int num1, num2, sum;
: This declares three integer variables:num1
,num2
, andsum
.printf("Enter first number: ");
: This line displays a prompt asking the user to enter the first number.scanf("%d", &num1);
: This line reads an integer from the user and stores it in the variablenum1
.printf("Enter second number: ");
: This line displays a prompt asking the user to enter the second number.scanf("%d", &num2);
: This line reads an integer from the user and stores it in the variablenum2
.sum = num1 + num2;
: This line calculates the sum ofnum1
andnum2
and assigns the result to the variablesum
.printf("Sum: %d\n", sum);
: This line prints the sum to the console.return 0;
: This line indicates that the program has executed successfully and returns a status code of 0.}
: This marks the end of the main function.