puzzle on c statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sridhard2406
    New Member
    • Dec 2007
    • 52

    puzzle on c statement

    Hi All.

    Can we write the below mentioned 4 lines statements into 1 line statements? .

    if(flag) // flag is a int variable
    return 1;
    else
    return 0;

    I dont want to use ternary operater. please let me know , is there any other way to do the same operation on single statement.

    Thanks,
    Sridhar.D
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Yes there is examine all the logical operators.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Is your goal to put this code snippet all on one line of the source file or to implement it in a single statement?

      In most cases, you can insert or remove newlines from C source code without altering the meaning of the code. This is a trivial answer to your question.

      Off-hand, I can think of three ways to accomplish this code snippet in a single statement. (One of these ways is with the ternary operator; which you prefer not to use.)

      Just because you can do something doesn't mean that you should. As far as I'm concerned, you shouldn't combine this snippet into a single statement unless that form makes it easier to understand what the function does. A lot depends on the meaning of the return values. Does this function return a true/false boolean value that is used with logical operators; or does this function return an integer value that is used with arithmetic operators?

      Comment

      • anurag275125
        New Member
        • Aug 2009
        • 79

        #4
        can anyone please give the answer?

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          if(! flag) return 0;

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by anurag275125
            can anyone please give the answer?
            I'm still waiting for clarification whether the goal is to put all of these statements on one line of source code or to reduce this snippet into a single statement.

            Comment

            • newb16
              Contributor
              • Jul 2008
              • 687

              #7
              The goal is to make the student to remember certain properties of implicit casting to bool of whatever-is-not-zero of certain unary operator that were explained in the class.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Originally posted by sridhard2406
                Code:
                if(flag) // flag is a int variable
                    return 1;
                else
                    return 0;
                Let's start by analyzing the original code snippet. The if statement executes the then leg if the argument is nonzero or it executes the else leg if the argument is zero. That is, we return "1" if flag is nonzero or we return "0" if flag is zero.

                You want to collapse this snippet into a single statement. The one thing we can say with certainty is that if this is possible, then the one statement has to be a return statement. That is,
                Code:
                return [I]expression[/I];
                Now the problem has shifted to finding an expression that evaluates to "1" when flag is any nonzero value, and that also evaluates to "0" when flag is zero. Banfa gave you an important clue in the first reply to this post: "examine all the logical operators". See if you can use the logical operators to construct the necessary expression.


                By the way,
                Originally posted by whodgson
                if(! flag) return 0;
                I'm afraid this isn't a complete answer because it doesn't provide a way to return "1".

                Comment

                • alexis4
                  New Member
                  • Dec 2009
                  • 113

                  #9
                  You could just write
                  Code:
                  return flag;

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Originally posted by alexis4
                    You could just write
                    Code:
                    return flag;
                    The original code returns only two distinct values (1 or 0). This suggestion doesn't map all nonzero flag values to "1".

                    Comment

                    • alexis4
                      New Member
                      • Dec 2009
                      • 113

                      #11
                      You are right, I was hasty. Flag is an int variable, so I missed the cast...

                      Comment

                      • donbock
                        Recognized Expert Top Contributor
                        • Mar 2008
                        • 2427

                        #12
                        Originally posted by alexis4
                        Originally posted by donbock
                        Originally posted by alexis4
                        You could just write
                        Code:
                        return flag;
                        The original code returns only two distinct values (1 or 0). This suggestion doesn't map all nonzero flag values to "1".
                        You are right, I was hasty. Flag is an int variable, so I missed the cast...
                        C89 does not have a boolean type. Programmers generally use int to hold true/false variables. The quoted messages illustrate a danger with that approach. Consider the following code snippet.
                        Code:
                        int flag = 2;
                        if (flag)
                           <[I]code executed when flag is true[/I]>
                        if (flag != false)
                           <[I]code executed when flag is not false, ie when it is true[/I]>
                        if (flag != true)
                           <[I]code executed when flag is not true, ie when it is false[/I]>
                        All three then-legs will be executed, so some parts of your program will act as if flag is true and others will act as if flag is false. This is a more pernicious bug than if flag were erroneously set to true when it should be false (or vice-versa). The way to avoid this problem is to never compare a logically boolean variable to "true".

                        How might that bad value have gotten into flag? The Standard guarantees that the results of the logical operators (!, ==, !=, <, etc) are always either 1 or 0 (true or false), so they didn't do it. The answer is that you must have put the illegal value in there explicitly. Perhaps you incremented the variable in a loop so it counted up past 1.

                        Comment

                        • mush1578
                          New Member
                          • Feb 2010
                          • 15

                          #13
                          if ur using templat function or macro's so u can do it in one statment

                          Comment

                          • alexis4
                            New Member
                            • Dec 2009
                            • 113

                            #14
                            Originally posted by donbock
                            C89 does not have a boolean type. Programmers generally use int to hold true/false variables.
                            I use 2 different compilers. The GCC based compiler doesn't accept bool. The other one does.
                            Either way, you can use a manual cast:
                            Code:
                            return(!(!flag));

                            That should work.

                            Comment

                            Working...