简体   繁体   English

根据用户的输入求出 c 中 2 个数字的总和

[英]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.这就是我正在尝试的,但是 23+23 以这种方式变成了 6422292。我找不到错误。 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嘿实际上错误在 printf() function

The & is your telling to print the value stored in the sum variable & 是您告诉打印存储在 sum 变量中的值

Make the following changes to your code对您的代码进行以下更改

printf("%d", sum);

Hope you got fixed the error

#include<stdio.h> #include<stdio.h>

int main() {诠释主要(){

int a, b;诠释一个,乙;

printf ("enter a\n"); printf ("输入\n");

scanf ("%d", &a); scanf ("%d", &a);

printf ("enter b\n"); printf ("输入 b\n");

scanf ("%d", &b); scanf ("%d", &b);

int sum = a + b;整数总和 = a + b;

printf ("the sum is: %d", sum); printf(“总和为:%d”,总和);

return 0;返回 0;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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