#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    What is the meaning of using ? and :


    for example what does this me
    fName = argc > 2 ? strdup(argv[2]) : strdup("outfp.dat");

    thanx for the help
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,612
    Rep Power
    1945
    It is a short syntax instead of using an if-statement. (Read the post by Jacques1 for better description)

    You can try to look it up
    Last edited by MrFujin; January 7th, 2014 at 04:03 AM.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by MrFujin
    It is a short syntax instead of using an if-statement.
    Not sure if that's a good description, because people do in fact often mistake it for "a short if statement".

    The ternary expression (it's not a statement) yields one of two values, depending on whether the condition is met or not. So it's kind of a conditional expression.

    For example:

    c Code:
    message =
    	age >= 21 ? "Have fun!" : "You are not allowed to play this game.";

    The only thing the ternary expression has in common with if statements is that both involve conditions.

    Comments on this post

    • MrFujin agrees
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    In C and C++ ?: is an operator. It is a ternary operator (meaning it has three operands). It is often erroneously referred to at "the ternary operator", as if that is what it is called - it merely happens to be the only ternary operator in C and C++. It is more correctly "the conditional operator".

    Because it is an operator rather than a control construct, it results in a value that can be assigned or used anywhere an expression is valid, such as function arguments and return values.

    Essentially an expression of the form:
    Code:
    <boolean expression> ? <expression1> : <expression2>
    evaluates and results in <expression1> is <boolean expression> is true, otherwise <expression2>.
    Last edited by clifford; January 7th, 2014 at 01:56 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0
    It is known as the ternary operators in C and C++ language. For ex:

    a=2>5? 100:200;
    printf(a);

    Now this C operator work like this if your condition part i.e (2>5) is true then the value before colon will print but if the condition is false then the value after the colon will print.

    So in this case 2 is not greater than 5 so 200 will be the output.
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by anilkumartgsb
    It is known as the ternary operators in C and C++ language.
    I guess you did not read my post which explains why this is not strictly correct.

IMN logo majestic logo threadwatch logo seochat tools logo