# Thread: Conditional Operator

1. ## Conditional Operator

I can not figure out why this program does not work the way I wont.
Code:
```#include <stdio.h>

int x, y;

int main(void)
{ printf("\nEnter two numbers");
scanf("%d %d",&x,&y);
printf("\n\n%d is bigger\n",((x>y)?x:y));
return 0;
}```
This will compile and run but when you put in two numbers, like 5,50, the program said that 5 is the bigger number.
Any number on the left of the comma is called the bigger number.

2. Sorry, but I cannot duplicate your problem. Input of 5 50 gives the expected result that 50 is bigger.

EDIT:
Just a moment, did you actually enter 5,50 rather than 5 50?

3. Try using two scanf statements.

Code:
```...
printf("Input first number: ");
scanf("%d", &x);
printf("Input second number: ");
scanf("%d", &y);
...```

4. It's likely that you entered 5,50 as suggested by laserlight.
since x and y are global variables, they are initialized to 0.
scanf() will fail when it reads ',' and return 1(check return value), x will be 5, y will still be 0.
hence you are getting 5 as result.

5. Thank you for posting, it is still not working for me. Even with splitting the scanf, I still get 5 is bigger than 50. I am going to export the code and run it on the desktop.

6. I suggest that you try:
Code:
```#include <stdio.h>

int main(void)
{
int x = 0, y = 0;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
if (x != y)
{
printf("\n%d is bigger than %d\n", ((x > y) ? x : y), ((x < y) ? x : y));
}
else
{
printf("\n%d is equal to %d\n", x, y);
}
return 0;
}```
This makes it clear that you are really getting "5 is bigger than 50" rather than "5 is bigger than 0".

7. Thank You, that is much clearer.

8. One last addition before ending this thread. I added a comma to the scanf line.
Code:
```#include <stdio.h>

int x, y;

int main(void)
{ printf("\nEnter two numbers");
scanf("%d,%d", &x, &y);
printf("\n\n%d is bigger\n", ((x>y)?x:y));
return 0;
}```
so now it reads %d,%d.

Once again, thanks for help.

Popular pages Recent additions