January 7th, 2014, 02:43 AM
What is the meaning of using ? and :
for example what does this me
fName = argc > 2 ? strdup(argv) : strdup("outfp.dat");
thanx for the help
January 7th, 2014, 03:27 AM
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.
January 7th, 2014, 03:52 AM
Not sure if that's a good description, because people do in fact often mistake it for "a short if statement".
Originally Posted by MrFujin
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.
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
January 7th, 2014, 01:54 PM
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:
evaluates and results in <expression1> is <boolean expression> is true, otherwise <expression2>.
<boolean expression> ? <expression1> : <expression2>
Last edited by clifford; January 7th, 2014 at 01:56 PM.
January 15th, 2014, 01:37 AM
It is known as the ternary operators in C and C++ language. For ex:
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.
January 15th, 2014, 01:52 PM
I guess you did not read my post which explains why this is not strictly correct.
Originally Posted by anilkumartgsb