d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Can Someone Tell Me What This Line Of C Means?
Add Reply New Topic New Poll
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Oct 26 2014 06:14pm
Someone can tell me what this line is equivalent to?

Code
float a = (argc - 2)?: strtod(argv[1], 0);



my guess:

Code
if (arc -2)
a = argc -2;
else
a = strtod(argv[1], 0);


Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 26 2014 06:19pm
Code
float a = (argc - 2)?: strtod(argv[1], 0);


unless ?: is some special operator i'm not familiar with, i'm guessing it's the ternary operator where the true case is omitted.

eg

if (arc -2)
// a = nothing?
else
a = strtod(argv[1], 0);

not sure if that even compiles tbh. is a not assigned anything?

/edit:
Code
http://en.wikipedia.org/wiki/%3F:#C



A GNU extension to C allows omitting the second operand, and using implicitly the first operand as the second also:
a = x ? : y;
The expression is equivalent to
a = x ? x : y;

This post was edited by carteblanche on Oct 26 2014 06:22pm
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Oct 26 2014 06:30pm
yea wikipedia said

Code
a = x ? : y;
is same as
Code
a = x ? x : y;


but I don't even understand how arc-2 is can be a condition

This post was edited by bakalolo on Oct 26 2014 06:32pm
Member
Posts: 24,488
Joined: Jul 11 2011
Gold: 1,272.50
Oct 26 2014 06:34pm
Quote (carteblanche @ 26 Oct 2014 17:19)
Code
float a = (argc - 2)?: strtod(argv[1], 0);


unless ?: is some special operator i'm not familiar with, i'm guessing it's the ternary operator where the true case is omitted.

eg

if (arc -2)
  // a = nothing?
else
a = strtod(argv[1], 0);

not sure if that even compiles tbh. is a not assigned anything?

/edit:
Code
http://en.wikipedia.org/wiki/%3F:#C



A GNU extension to C allows omitting the second operand, and using implicitly the first operand as the second also:
a = x ? : y;
The expression is equivalent to
a = x ? x : y;


It's called a ternary operator...

This post was edited by HighschoolTurd on Oct 26 2014 06:34pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 26 2014 06:34pm
Quote (bakalolo @ Oct 26 2014 08:30pm)
yea wikipedia said

Code
a = x ? : y;
is same as
Code
a = x ? x : y;


but I don't even understand how arc-2 is can be a condition


0 and null return false, everything else returns true iirc

eg it's perfectly fine to say something like:

if (1)

This post was edited by carteblanche on Oct 26 2014 06:36pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 26 2014 07:03pm
Quote (bakalolo @ Oct 26 2014 08:30pm)
yea wikipedia said

Code
a = x ? : y;
is same as
Code
a = x ? x : y;


but I don't even understand how arc-2 is can be a condition



Argc is the number of arguments the application was supplied. 0 and negatives are false, while positives are true. So if argc >=3 then that will return true.

At least if I remember correctly.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 26 2014 07:06pm
Quote (AbDuCt @ Oct 26 2014 09:03pm)
Argc is the number of arguments the application was supplied. 0 and negatives are false, while positives are true. So if argc >=3 then that will return true.

At least if I remember correctly.


hmm i'm pretty sure negatives are true. but it's been a while.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 26 2014 07:14pm
Quote (carteblanche @ Oct 26 2014 09:06pm)
hmm i'm pretty sure negatives are true. but it's been a while.


Heh I have no idea. That function would break if he misses arguments as it would return true for -2 and -1.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll