programming a "conditional" function

This is a discussion on programming a "conditional" function within the C Programming forums, part of the General Programming Boards category; I'm supposed to write code for a function that does the following conditional statement: x ? y : z I ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    programming a "conditional" function

    I'm supposed to write code for a function that does the following conditional statement:

    x ? y : z

    I found an explanation stating that this means:

    If x is true, then execute y, otherwise execute z.

    However, the example given is conditional(2,4,5) = 4

    I don't see how there is a statement to evaluate to true or false.

    I have no clue why the answer is 4, can anyone explain it?

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678

    Talking I am sure someone will answer before me

    They are all pretty excited about simple problems that newbies have

    I will give you a hint: to implement your function just put return in front of the conditional statement.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    4
    That doesn't help me understand how conditional works...

    P.S. I'm not so much a newbie. Most of my experience is with C++ and not C and I have never encountered this conditional function before so I'm looking for a good explanation of what it does...

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    The answer is 4 because, in C, every value different than 0 is "true". In your example, x is, indeed, different than 0, so the answer is 4. If it were zero, the answer would be 5.
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by manav View Post
    They are all pretty excited about simple problems that newbies have
    Guess what! I am also pretty excited about it

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    4
    Quote Originally Posted by Mr_Miguel View Post
    The answer is 4 because, in C, every value different than 0 is "true". In your example, x is, indeed, different than 0, so the answer is 4. If it were zero, the answer would be 5.
    Got it. Thank you.

    EDIT: Do negative numbers still count as true or does it have to be greater than 0?

  7. #7
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Yes, negative numbers are also evaluated as "true".
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    4
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21