Reputation: 152
The else part in ternary operator which is a printf statement is not working in the code, Is the syntax correct??Or some silly mistake??
#include<stdio.h>
#define isNegative(x) x<0 ? 1 : 0
#define isPositive(x) isNegative(x) ? 0 : 1
#define isEven(x) x%2 ? 0 : 1
#define isOdd(x) isEven(x) ? 0 : 1
main(){
int n,ch;
do{
printf("Enter a number\n");
scanf("%d",&n);
printf("Choose an operation :\n 1.isEven\n 2.isOdd\n 3.isPositive\n 4.isNegative\n");
scanf("%d",&ch);
switch(ch){
case 1 :isEven(n) ? printf("Its even number\n") : printf("Its not an even number\n") ;
break;
case 2 :isOdd(n) ? printf("Its odd number\n") : printf("Its not an odd number\n") ;
break;
case 3 :isPositive(n) ? printf("Its a positive number\n") : printf("Its not a positive number\n");
break;
case 4 :isNegative(n) ? printf("Its a negative number\n") : printf("Its not a negative number\n");
break;
default : printf("Enter valid option\n");
break;
}
printf("Press 5 to continue or 6 to exit\n");
scanf("%d",&ch);
}while(ch!=6);
}
Is the logic of the code correct? contents of header file
Upvotes: 0
Views: 80
Reputation: 1159
The ternary operator isn't like an if
-then
-else
statement; it's an expression in and of itself, which means its value is whatever gets evaluated in the usual fashion. You can't see it in your code because the result of the ternary operator expressions you used isn't being captured by anyone. It's what in C++ is called an rvalue
.
You can actually prove this to yourself by doing this:
size_t i = 1;
(i == 2) ? printf("hello\n") : printf("goodbye\n")
Output:
goodbye
Here again we're just evaluating the predicate and evaluating the "else" expression. The output, not surprisingly, is goodbye
. This isn't the whole story though.
A lot of people don't realize printf
has a return value. It returns the number of characters written to stdout, or in the case of fprintf
to whichever output stream you specify. Like the scanf
functions, the printf
s will return a negative number on an error. This is why it's important to check the return value of scanf. Unfortunately many people don't realize these functions have return values, so obviously they don't check what they don't know exists.
With this next example though, you can clearly see the return value of the ternary expression.
size_t i = 1;
int result = (i == 2) ? printf("hello\n") : printf("goodbye\n");
printf("Result: %d\n", result);
Output:
goodbye
Result: 8
Going back to the actual ternary operator, I want to stress that's its an expression. You see things like this very often in functional programming languages, albeit not in the same "shape", if you will. The fact that the printf
function is printing to stdout is what's known as a side-effect, and the concept of reducing side-effects as much as possible while still having a useful programming language is one of the foundations of functional programming.
To answer your specific question:
Is the logic of the code correct?
I reformatted some of the code and added a signficant amount of parentheses. You have to be really careful when you're working with both macros and ternary operators to get the parentheses right or you'll get errors like that.
#include <stdio.h>
#define isNegative(x) x<0 ? 1 : 0
#define isPositive(x) isNegative(x) ? 0 : 1
#define isEven(x) x%2 ? 0 : 1
#define isOdd(x) isEven(x) ? 0 : 1
int main()
{
int n, ch;
do {
printf("Enter a number\n");
scanf("%d", &n);
printf("Choose an operation :\n 1.isEven\n 2.isOdd\n 3.isPositive\n 4.isNegative\n");
scanf("%d", &ch);
switch (ch) {
case (1): ((isEven(n)) ? printf("Its even number\n") : printf("Its not an even number\n"));
break;
case (2): ((isOdd(n)) ? printf("Its odd number\n") : printf("Its not an odd number\n"));
break;
case (3): ((isPositive(n)) ? printf("Its a positive number\n") : printf("Its not a positive number\n"));
break;
case (4): ((isNegative(n)) ? printf("Its a negative number\n") : printf("Its not a negative number\n"));
break;
default: printf("Enter valid option\n");
break;
}
printf("Press 5 to continue or 6 to exit\n");
scanf("%d",&ch);
} while(ch != 6);
return EXIT_SUCCESS;
}
Execution:
Enter a number
5
Choose an operation :
1.isEven
2.isOdd
3.isPositive
4.isNegative
4
Its not a negative number
Press 5 to continue or 6 to exit
5
Enter a number
3
Choose an operation :
1.isEven
2.isOdd
3.isPositive
4.isNegative
3
Its a positive number
Press 5 to continue or 6 to exit
Hope this helps, man, good luck. Let me know if you have any questions.
Upvotes: 1