Thread: Is there a difference between...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    Is there a difference between...

    c = (a) ? b : d++; and c = (a==c) ? b : d++;

    or they are completely different?

    {a,b,c,d} are random numbers.
    I know what the second one means i just cant understand if the first one is any different.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Do you understand what the ternary operator does? Also, remember that any non-zero value is considered true in C. Maybe it would be easier if you converted these into their if-else equivalents and then tried to answer it.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    They're different. The condition for the first one only checks if a is anything except 0, and the second one checks if a equals c.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by anduril462 View Post
    Do you understand what the ternary operator does? Also, remember that any non-zero value is considered true in C. Maybe it would be easier if you converted these into their if-else equivalents and then tried to answer it.
    First of all thanx for the reply.
    Yeap i know what the ternary conditional operator does.
    The thing is that i cant figure out what the condition on the first one c = (a) ? b : d++; is.
    How can a plain (a) be any condition?Is it a shortcut for (c==a)?
    Last,i cant convert it into if-else equivalents since i i dont know how and im pretty new working with C language(noob actually).

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Anything non-zero is considered true. So:
    Code:
    if( a )
    Means:
    Code:
    if( a != 0 )
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by itsme86 View Post
    They're different. The condition for the first one only checks if a is anything except 0, and the second one checks if a equals c.
    aha!this makes sense.Thanx a lot!

    @Quzah:thanx bro!
    Last edited by jimbo_1; 03-25-2011 at 04:51 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by jimbo_1 View Post
    Last,i cant convert it into if-else equivalents since i i dont know how and im pretty new working with C language(noob actually).
    Did you read the wikipedia article I linked you to (red text)? It shows how to convert the ternary expression to an if-else. Go read it now, since it really helps with complex ?: statements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DIfference between , and ;
    By rocketman03 in forum C++ Programming
    Replies: 6
    Last Post: 10-21-2009, 09:46 AM
  2. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  3. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  4. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  5. What is the Difference between ANSI C and non-ANSI C ?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-24-2001, 06:55 AM