Thread: Ternary operator unexpected result

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    6

    Ternary operator unexpected result

    Hello guys, I have into a header 2 defined string macros where I am using them as a return value for a ternary operator i.e.:

    --- foo.h ---
    ...
    #define STR1 "CORRECT"
    #define STR2 "WRONG"
    ...


    --- foo.c ---
    #include "foo.h"
    ...


    char ch = 'A', ch2 = 'a';
    char *result = ch == ch2 ? STR1 : ((ch == (ch2 - 32) ? STR1 : STR2);


    printf("Result: %s\n, result);
    ...

    The thing is the returned value is STR2 when either first condition is True or the second one in parentheses. I believe that should be not the case but maybe I am missing some knowledge with ternary? or preprocessor macros.
    I tested the result of this expression without the macros just with read only strings i.e.:

    char *result = ch == ch2 ? "CORRECT" : ((ch == (ch2 - 32) ? "CORRECT" : "WRONG");

    and the return value is as expected.
    Any reasoning for this?
    Last edited by WhiteDeviL; 08-16-2021 at 05:34 AM.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    291
    What are the ASCII codes for 'a' and 'A'?

    And when you plug those values into the expression?

  3. #3
    Registered User
    Join Date
    Mar 2021
    Posts
    6
    Well my bad I did a mistake when I initialized ch2 in my example

    I edited the question so everything I mentioned still implies about the reasoning behind this behavior.

  4. #4
    Registered User
    Join Date
    Mar 2021
    Posts
    6
    The thing I care the most is about the different behavior I am getting when I use the macros as return values from the ternary operator.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,351
    Quote Originally Posted by WhiteDeviL
    The thing I care the most is about the different behavior I am getting when I use the macros as return values from the ternary operator.
    That's an interesting claim, so I wrote this program to check:
    Code:
    #include <stdio.h>
    
    #define STR1 "CORRECT"
    #define STR2 "WRONG"
    
    int main(void)
    {
        char ch = 'A', ch2 = 'a';
        char *result = ch == ch2 ? STR1 : ((ch == (ch2 - 32)) ? STR1 : STR2);
        //char *result = ch == ch2 ? "CORRECT" : ((ch == (ch2 - 32)) ? "CORRECT" : "WRONG");
    
        printf("Result: %s\n", result);
    
        return 0;
    }
    Compiling the above program with gcc 9.3.0 and then running it, I get the output:
    Code:
    Result: CORRECT
    I comment out the first declaration of result and uncomment the other declaration of result, compile, and run to get the output:
    Code:
    Result: CORRECT
    So I am afraid that I cannot replicate your observation.

    I suggest that you do the same with your compiler. Chances are, you will get the same result, i.e., when you saw the different behaviour, it is because you changed something else but did not notice.

    (For example, I note that your example code doesn't actually compile due to typo errors, indicating that you did not actually compile it before posting.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,733
    Works for me.
    Code:
    #include <stdio.h>
    
    #define STR1 "CORRECT"
    #define STR2 "WRONG"
    
    void foo( ) {
      char ch = 'A', ch2 = 'a';
      char *result = ch == ch2 ? STR1 : ((ch == (ch2 - 32) ? STR1 : STR2));
      printf("Result: %s\n", result);
    }
    void bar() {
      char ch = 'a', ch2 = 'A'; // swapped
      char *result = ch == ch2 ? STR1 : ((ch == (ch2 - 32) ? STR1 : STR2));
      printf("Result: %s\n", result);
    }
    void baz() {
      char ch = 'A', ch2 = 'b'; // different
      char *result = ch == ch2 ? STR1 : ((ch == (ch2 - 32) ? STR1 : STR2));
      printf("Result: %s\n", result);
    }
    
    int main()
    {
      foo();
      bar();
      baz();
      return 0;
    }
    It's hard to say what you're up to when even your single line of code won't compile properly.
    Code:
    foo.c: In function ‘main’:
    foo.c:9:70: error: expected ‘)’ before ‘;’ token
       char *result = ch == ch2 ? STR1 : ((ch == (ch2 - 32) ? STR1 : STR2);
                                                                          ^
    And it's nothing to do with whether you #include things or not.

    If you're still stuck, you need to post actual code you compiled and ran.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2021
    Posts
    6
    Well guys I do really appreciate your effort and time for answering!
    I am pretty sorry for the examples and the typo errors..
    There was a mistake on #define directives at .h file..
    So nothing wrong with my actual code and ternary operator except that I accidentally reversed the two macros
    Thanks again for your time and effort answering!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bitwise OR unexpected result
    By nappaji in forum C Programming
    Replies: 2
    Last Post: 10-12-2016, 11:29 AM
  2. Ternary Operator to choose which operator to use
    By cncool in forum C Programming
    Replies: 7
    Last Post: 06-27-2011, 01:35 PM
  3. unexpected result
    By abotaha in forum C++ Programming
    Replies: 14
    Last Post: 11-17-2009, 08:32 PM
  4. No error, but the result is unexpected
    By Nimbuz in forum C Programming
    Replies: 10
    Last Post: 07-24-2009, 03:10 PM
  5. an unexpected result.
    By System_159 in forum C Programming
    Replies: 7
    Last Post: 01-22-2008, 07:05 AM

Tags for this Thread