为什么我的代码没有给出正确的输出?谁能提供一个提示?

时间:2021-07-31 07:21:13

标签: c function

所以基本上我写了这段代码来打印更大的数字,但它不起作用。我是 C 的新手,这让我很困惑

#include <stdio.h> 
int greater(int a, int b); 
int main() 
{ 
int a,b,x;
printf("\n Enter two numbers:"); 
scanf("%d %d ",&a, &b); 
x=greater(a, b); 
printf("\n The greatest number is:%d", x); 
return 0;
} 
int greater(int x, int y) 
{  int great;
    if(x>y){
        great=x;
    }
    else 
    {
        great=y;
    }
    return great;
    
}```

1 个答案:

答案 0 :(得分:2)

问题是scanf中的尾随空格,切换到:

printf("\n Enter two numbers:"); 
scanf("%d %d",&a, &b);
x=greater(a, b); 

了解原因:What is the effect of trailing white space in a scanf() format string?

相关问题