简体   繁体   中英

find the sum of 2 numbers in c with input from user

#include<stdio.h>
int main(){
int num1=0;
int num2=0;
int sum=0;
printf("enter 2 numbers\n");
scanf("%d %d",&num1,&num2);
sum=num1+num2;
printf("%d",&sum);
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.

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

printf("%d",&sum);

It should be

printf("%d", sum);

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

#include<stdio.h>

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM