Thread: Having a hard time interpreting this code

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Having a hard time interpreting this code

    I am going over questions from tests from previous semester of what the test I am going to take might be like and one question I do not understand is this:

    What is the output of the following code segment?
    Code:
    #include <stdio.h>
    
    int main(void);
    {
          int a=6, b=4;
    
          if (b = 0)
    
            printf("One\n");
    
          else if (a = 2)
    
            printf("Two\n");
    
          else if (a*b/5 > 4)
    
            printf("Three\n");
    
          else
    
            printf("Four\n");
    
    }
    when I run it in the compiler, the answer is "Two." How do you interpret this because I do not understand how the answer is "Two?"

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It prints that because the code is broken.
    Code:
    else if (a = 2)
    That does not compare "a" to the number 2. It assigns "a" the value of 2 (notice how it is using "=" instead of "=="). So basically, that code is the same as:
    Code:
    else if (2)
    which will always evaluate to true.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by bithub View Post
    It prints that because the code is broken.
    Code:
    else if (a = 2)
    That does not compare "a" to the number 2. It assigns "a" the value of 2 (notice how it is using "=" instead of "=="). So basically, that code is the same as:
    Code:
    else if (2)
    which will always evaluate to true.
    what is throwing me off is the a=6 and b=4. Am I supposed to plug that in for the a and b in the if-else if statement or no? Because that is where I am confused if they give me something like this on a test and I have to look at this and figure it out.

    I understand what you said about the "=" and "==." I am still not getting it because of the int a=6 and b=4.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    AFAIK the assignment inside the paranthesis is done before a is evaluated. a is 6 until you assign 2 to it, after that, you test if it is 2, which it is, so "two" is printed. Actually, I think it would not matter what value a has, as long as it's not zero.
    Last edited by Subsonics; 09-29-2009 at 12:08 PM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Subsonics View Post
    AFAIK the assignment inside the paranthesis is done before a is evaluated. a is 6 until you assign 2 to it, after that, you test if it is 2, which it is, so "two" is printed.
    Wrong. As bithub points out, there is no such test. Note that assignment as a condition will return the value of the expression.

    a = 2; // assigns the value 2 to a. ALWAYS TRUE*
    a == 2; // TRUE if the value of a is 2, FALSE otherwise.

    *unless the right side is 0, because the actual value of the expression is the final value of the left hand side, so eg. (b = 0) == 0, and 0 is FALSE.
    Last edited by MK27; 09-29-2009 at 12:11 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by MK27 View Post
    Wrong. As bithub points out, there is no such test. Note that assignment as a condition will always succeed.

    a = 2; // assigns the value 2 to a. ALWAYS TRUE.
    a == 2; // TRUE if the value of a is 2, FALSE otherwise.
    Ha ha, I just edited my post. It's always true as long as it's not zero. I realize that it's the same as: if(a).

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by BLG View Post
    what is throwing me off is the a=6 and b=4. Am I supposed to plug that in for the a and b in the if-else if statement or no? Because that is where I am confused if they give me something like this on a test and I have to look at this and figure it out.

    I understand what you said about the "=" and "==." I am still not getting it because of the int a=6 and b=4.
    The a=6 and b=4 are just there to confuse you. They don't actually have any effect on the output, because the values of a and b are changed.

    Quote Originally Posted by Subsonics
    after that, you test if it is 2, which it is, so "two" is printed.
    Not quite. The test is if it is non-zero, not two.
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes, I realized this and changed my post. lol

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by bithub View Post
    The a=6 and b=4 are just there to confuse you. They don't actually have any effect on the output, because the values of a and b are changed.
    But wouldn't "two" get printed even if 2 wasn't assigned to a? since what is tested is only if a is not zero.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Subsonics View Post
    But wouldn't "two" get printed even if 2 wasn't assigned to a? since what is tested is only if a is not zero.
    if(a) would be true, yes, cause it is non-zero. Unless a is 0.

    I have an old keyboard with a giant

    ==

    at the top in permanent marker because of this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by MK27 View Post
    if(a) would be true, yes, cause it is non-zero. Unless a is 0.

    I have an old keyboard with a giant

    ==

    at the top in permanent marker because of this.
    Yes, but my point was that the reason that the initial value of a doesn't have an effect on the output is not that it is changed to 2.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Subsonics View Post
    Yes, but my point was that the reason that the initial value of a doesn't have an effect on the output is not that it is changed to 2.
    Your point is wrong. The reason the initial value of a doesn't have any effect is precisely due to the fact that it was changed to 2. If the conditional was just:
    Code:
    if (a)
    instead of
    Code:
    if (a=2)
    then the initial value of a is what would determine the output of the conditional.
    bit∙hub [bit-huhb] n. A source and destination for information.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by bithub View Post
    Your point is wrong. The reason the initial value of a doesn't have any effect is precisely due to the fact that it was changed to 2. If the conditional was just:
    Code:
    if (a)
    instead of
    Code:
    if (a=2)
    then the initial value of a is what would determine the output of the conditional.
    I did not say "doesn't have any effect" I said "doesn't have any effect on the output", if a is 2 or 6 wouldn't matter the printf statement would still get executed.

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    It is a little confusing but I think I am starting to get it.

    Thank You For Your Help Guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. how do u know when 2 stop spending time on one code?
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 07-26-2003, 09:54 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 10-03-2002, 03:04 PM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM