Questions & Answers

find the sum of 2 numbers in c with input from user
int main(){
int num1=0;
int num2=0;
int sum=0;
printf("enter 2 numbers\n");
scanf("%d %d",&num1,&num2);
return 0;

This is what i am trying but 23+23 is coming out to be 6422292 in this way.I cant find the error. Please help.

2023-01-11 09:12:54
Rishi Gandhi, Why did you code and put a & in printf("%d",&sum);?
Answers(3) :

Hey actually the error is in the printf() function

The & is your telling to print the value stored in the sum variable

Make the following changes to your code

printf("%d", sum);

Hope you got fixed the error

Do NOT put an "address of" operator (&) on this line:


It should be

printf("%d", sum);


int main() {

int a , b;

printf ("enter a\n");

scanf ("%d", &a);

printf ("enter b\n");

scanf ("%d", &b);

int sum = a + b;

printf ("the sum is : %d", sum);

return 0;