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;

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

fireball003

Full Member level 3
Full Member level 3
Joined
Oct 28, 2005
Messages
181
Helped
8
Reputation
16
Reaction score
3
Trophy points
1,298
Activity points
3,157
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

wwfeda

Junior Member level 3
Junior Member level 3
Joined
Dec 17, 2005
Messages
29
Helped
4
Reputation
8
Reaction score
0
Trophy points
1,281
Activity points
1,632
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.
 

slavko

Member level 2
Member level 2
Joined
Sep 1, 2004
Messages
46
Helped
7
Reputation
14
Reaction score
0
Trophy points
1,286
Activity points
369
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

ashish sharma

Full Member level 2
Full Member level 2
Joined
Jul 6, 2004
Messages
132
Helped
9
Reputation
18
Reaction score
5
Trophy points
1,298
Activity points
1,120
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.
 

mobile-it

Advanced Member level 1
Advanced Member level 1
Joined
Apr 24, 2004
Messages
464
Helped
22
Reputation
44
Reaction score
8
Trophy points
1,298
Activity points
3,344
Search for conditional expression in the C bible (aka Kerningham and ritchie) and you will find the answer by yourself and learn more!!!
 

btbass

Advanced Member level 5
Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,896
Helped
438
Reputation
880
Reaction score
288
Trophy points
1,363
Location
Oberon
Activity points
12,887
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;
 

echo47

Advanced Member level 6
Advanced Member level 6
Joined
Apr 7, 2002
Messages
3,933
Helped
638
Reputation
1,274
Reaction score
90
Trophy points
1,328
Location
USA
Activity points
33,176
Even better, if True is 1 and False is 0, then:
return A == B;
is neater than:
return (A == B) ? True : False;
 

swapgo

Full Member level 2
Full Member level 2
Joined
Jun 24, 2004
Messages
128
Helped
3
Reputation
6
Reaction score
1
Trophy points
1,298
Location
India
Activity points
1,244
This is also called as ternary operator.
 

btbass

Advanced Member level 5
Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,896
Helped
438
Reputation
880
Reaction score
288
Trophy points
1,363
Location
Oberon
Activity points
12,887
Nice one echo47, but it was just an example.
You can of course also nest them.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top