Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

? and : in C programming

Status
Not open for further replies.

feiutm9898

Full Member level 4
Full Member level 4
Joined
May 31, 2004
Messages
224
Helped
4
Reputation
8
Reaction score
0
Trophy points
1,296
Location
Singapore
Activity points
2,027
Hi.

What is the meaning of "?" and ":" in C programming.

Hope can get your comment.

-----------------------------------------------------------------

Job[ id ].Flag.process= ( Option & Mask ) ? 1 : 0;

-----------------------------------------------------------------
 

Hi,
It is the short form of if else condition. that is -

if(Option & Mask){
Job[ id ].Flag.process=1;
}
else{
Job[ id ].Flag.process=0;
}
 

    feiutm9898

    Points: 2
    Helpful Answer Positive Rating
if true, use the first one. if false, use the second one.

If you don't understand or aren't comfortable in using it, then use if else statements. Anything that makes your code readable is good. Writing a compact code aren't necessary good, especially in the long run.
 

The Conditional Operator:


General Comments:

This operator takes three operands, each of which is an expression. They are arranged as follows:

expression1 ? expression2 : expression3

The value of the whole expression equals the value of expression2 if expression1 is true. Otherwise, it equals the value of expression3.

Examples:

(5 > 3) ? 1 : 2 has the value 1.

(3 > 5) ? 1 : 2 has the value 2.

(a > b) ? a : b has the value of the larger of a or b.
 

    feiutm9898

    Points: 2
    Helpful Answer Positive Rating
slavako explained it in a very right manner!

simply you can replace the two by if and else if exp 1 is true the is equivalent to exp2 else its equal to exp3.
 

Search for conditional expression in the C bible (aka Kerningham and ritchie) and you will find the answer by yourself and learn more!!!
 

This is a handy construct for a conditional return statement.
for example,

return (A == B) ? True : False;

is neater then

if(A == B)
return True;
else
return False;
 

Even better, if True is 1 and False is 0, then:
return A == B;
is neater than:
return (A == B) ? True : False;
 

This is also called as ternary operator.
 

Nice one echo47, but it was just an example.
You can of course also nest them.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top