0

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

Deepak Pawade
  • 150
  • 1
  • 13
  • 1
    This might help - https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer – MayurK Apr 14 '19 at 07:38
  • 2
    "*is not working*" is the more or less worth trouble report one can give. Please be more specific. What do you get? What do you expect to get? – alk Apr 14 '19 at 07:38
  • @alk the second statement replacing else is not working... The true condition works but not the false one.. except the last one – Deepak Pawade Apr 14 '19 at 07:59
  • why not just `printf("It's %san even number\n", isEven(n) ? "" : "not ")`? But in practice it's often more useful to use `printf("It's an %s number\n", isEven(n) ? "even" : "odd")` – phuclv Apr 14 '19 at 09:05

1 Answers1

1

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 printfs 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.

  • 2
    I'd have put the parentheses around the macro definitions, like this: `#define (isNegative(x) x<0 ? 1 : 0)` – alk Apr 14 '19 at 08:18
  • @Jose Fernando Lopez Fernandez Thanks,its working.. but I added the parenthesis in those macros instead of adding them in main method and it worked too... How are those parenthesis affecting the return values?? – Deepak Pawade Apr 14 '19 at 08:26
  • 1
    The thing is, the C preprocessor is finding and replacing those macros, it's not doing anything special. So the problem is that while sometimes you can get away with less than perfect parentheses discipline, sometimes you're not so lucky. Those errors can be especially hard to find because the compiler and you are looking at different source files, so a good thing to try if you run out of ideas is to just run the preprocessor without compiling and see if the preprocessed source file actually looks like what it's supposed to. – Jose Fernando Lopez Fernandez Apr 14 '19 at 08:33
  • 2
    [The need for parentheses in macros in C](https://stackoverflow.com/q/10820340/995714) – phuclv Apr 14 '19 at 09:03