simple c program keeps crashing

不想你离开。 提交于 2019-12-02 11:37:29

问题


#include <stdio.h>
int main(void)
{
int a,b,c;
printf("Enter values of a,b,c:");
scanf("%d %d %d",a,b,c);

printf("\nDescending order of the numbers entered:");

/*Test for Biggest Number*/
if((a>b)&&(a>c))
 printf("%d",a);
else if((b>a)&&(b>c))
 printf("%d",b);
else if((c>a)&&(c>b))
 printf("%d",c);

 /*Test for Second Biggest Number*/
 if((a>b&&a<c)||(a<b&&a>c))
  printf("%d",a);
 else if((b>a&&b<c)||(b<a&&b>c))
  printf("%d",b);
 else if((c>a&&c<b)||(c<a&&c>b))
  printf("%d",c);

/*Test for Smallest Number*/
if((a<b)&&(a<c))
 printf("%d",a);
else if((b<a)&&(b<c))
 printf("%d",b);
else if((c<a)&&(c<b))
 printf("%d",c);

 return 0;
}

this is a c program in which 3 numbers are entered and the program prints the in descending order. i compiled the program and the ran the program.after entering the three numbers the program would just crash. is there something wrong with my code or do i have to add something?


回答1:


That's because you are not passing the address of your variables to scanf. Change

scanf("%d %d %d",a,b,c) 

to

scanf("%d %d %d",&a,&b,&c)



回答2:


Try to use:-

scanf("%d %d %d",&a,&b,&c)

instead of

scanf("%d %d %d",a,b,c) 

as & refers to the address of your variables.

In C the parameters are passed by value so you need to pass the address (or pointer). When you pass the address (or pointer) then scanf knows where it has to put the value.



来源:https://stackoverflow.com/questions/19364298/simple-c-program-keeps-crashing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!