Presika Subedi
Presika Subedi

Reputation: 13

why is my code not giving the proper output?Can anyone provide a hint?

so basically I wrote this code to print the greater number but it is not working.I am new to C and this confuses me a lot

#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;
    
}```

Upvotes: 1

Views: 42

Answers (1)

David Ranieri
David Ranieri

Reputation: 41017

The problem is the trailing white space in scanf, switch to:

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

See why: What is the effect of trailing white space in a scanf() format string?

Upvotes: 2

Related Questions