Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Explanation of ? operator missing in reference guide

5 views
Skip to first unread message

Skybuck Flying

unread,
Mar 31, 2009, 4:33:10 AM3/31/09
to
Explanation of ? operator missing in reference guide:

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/1.5.html

(No wonder it's a popular obfuscation use ! ;))

Bye,
Skybuck.


Skybuck Flying

unread,
Mar 31, 2009, 4:40:34 AM3/31/09
to
Not only is it not documented in the reference guide but it's syntax/use is
counter-intuitive:

http://www.eskimo.com/~scs/cclass/int/sx4eb.html

One would expect false to be represent by 0 and true to be represented by 1

Therefore

A ? B : C

The logical order would be:

A ? 0 : 1

But instead it's

A ? 1 : 0

One could argue that this resembles the if-else statement, though the
if-else statement is usually on seperate lines while the question mark is
represented to be on a single line and therefore a completely different
language element thus special treatment is warranted.

I find

A ? 0 : 1 more logical in this reguard when seeing it on a single line.

Ofcourse this argumenting is useless.... since it's already part of the
""""""""""standard""""""""""

Just taking a piss at the language itself, may I ? ;)

Bye,
Skybuck.


Joe Wright

unread,
Mar 31, 2009, 10:21:56 AM3/31/09
to
You don't need permission, of course. Your argument seems nonsense.

int A, B;
A = 1; /* true */
B = A ? 1 : 0; /* B follows A */

The above is perfectly logical. Same as..

if (A)
B = 1;
else
B = 0;

The language is just fine.

--
Joe Wright
"Memory is the second thing to go. I forget what the first is."

Lew Pitcher

unread,
Mar 31, 2009, 10:55:28 AM3/31/09
to
On March 31, 2009 04:40, in comp.lang.c, Skybuck Flying
(Blood...@hotmail.com) wrote:

> Not only is it not documented in the reference guide but it's syntax/use
> is counter-intuitive:
>
> http://www.eskimo.com/~scs/cclass/int/sx4eb.html
>
> One would expect false to be represent by 0 and true to be represented by
> 1

Close enough. A non-zero value is, when evaluated as a condition, taken as
a "TRUE" value, while a zero value is, when evaluated as a condition, taken
as a "FALSE" value.

Your expectations are close enough.

> Therefore
>
> A ? B : C
>
> The logical order would be:
>
> A ? 0 : 1

I don't see how you got that. The trinary operator, by definition (and by
analogy to the "if" statement) puts the "true" actions ahead of the "false"
actions.

Either your example is irrelevant (the "true" and "false" actions, after
all, are arbitrary as far as the language is concerned, and only
have "meaning" in the context of the logic in which the trinary operator is
used. In other words, if it is your intent as a programmer to present an
rvalue of 0 when a certain condition is true, and 1 when it is not, then
the above example is correct, otherwise, it is not), or your example is
contrary to the language.

You seem to be saying that (in pseudocode)
IF condition is TRUE
THEN
result is FALSE
OTHERWISE
result is TRUE
FI
is the "logical order" of things, or perhaps that (again in pseudocode)
IF condition is FALSE
THEN
result is FALSE
OTHERWISE
result is TRUE
FI
is the logical order. But, the trinary operator neither orders the false
condition first nor does it evaluate the falseness of the condition.


> But instead it's
>
> A ? 1 : 0

If you mean something like


B = A ? 1 : 0;

is like
B = (A != 0)
then, you are correct.

> One could argue that this resembles the if-else statement, though the
> if-else statement is usually on seperate lines while the question mark is
> represented to be on a single line and therefore a completely different
> language element thus special treatment is warranted.

Nonsense!!

B = A ? /* test truthfulness of A */
1 : /* A is TRUE - result is 1 */
0 ; /* A not TRUE - result is 0 */

See? Three lines. Just the same as


if (A)
B = 1;
else B = 0;

> I find
>
> A ? 0 : 1 more logical in this reguard when seeing it on a single line.

/You/ may find the contra-expression "more logical", but most others don't.
Your failings are none of our business. Why do you feel the need to admit
them to us?

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------


Keith Thompson

unread,
Mar 31, 2009, 11:32:31 AM3/31/09
to
Joe Wright <joeww...@comcast.net> writes:
> Skybuck Flying wrote:
[...]

>> Just taking a piss at the language itself, may I ? ;)
>>
> You don't need permission, of course. Your argument seems nonsense.
[...]

You might want to take a look at Skybuck Flying's posting history on
other newsgroup.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Skybuck Flying

unread,
Mar 31, 2009, 12:01:55 PM3/31/09
to
It's very easy to explain the logic behind my reasoning:

1 comes after 0.

So

A ? 0 : 1

Is the most logical way to view things ;)

Now imagine if it was a case statement operator:

A ? 0 : 1 : 2 : 4 : 5

^ Highly logical.

I rest my case ;)

Bye,
Skybuck.


Skybuck Flying

unread,
Mar 31, 2009, 12:06:03 PM3/31/09
to

"Joe Wright" <joeww...@comcast.net> wrote in message
news:6bedneOa-Nplu0_U...@giganews.com...

Another way to view it is:

A ? initializer : action.

if A is not initialized then initialize.
if A is initialized then take action.

Thus the false would be first, the true would be second.

Bye,
Skybuck.


Bartc

unread,
Mar 31, 2009, 3:04:00 PM3/31/09
to

"Skybuck Flying" <Blood...@hotmail.com> wrote in message
news:add9f$49d23e67$d5337e4d$10...@cache2.tilbu1.nb.home.nl...

> It's very easy to explain the logic behind my reasoning:
>
> 1 comes after 0.
>
> So
>
> A ? 0 : 1
>
> Is the most logical way to view things ;)
>
> Now imagine if it was a case statement operator:
>
> A ? 0 : 1 : 2 : 4 : 5

You need C's :? operator which does exactly what you want:

A : 0 ? 1

--
Bartc

Richard Tobin

unread,
Mar 31, 2009, 4:52:59 PM3/31/09
to
In article <add9f$49d23e67$d5337e4d$10...@cache2.tilbu1.nb.home.nl>,
Skybuck Flying <Blood...@hotmail.com> wrote:

>Now imagine if it was a case statement operator:
>
>A ? 0 : 1 : 2 : 4 : 5

Algol 68 has an abbreviated case statement similar to that:

(A | 0, 1, 2, 4, 5 | -1)

(-1 is the default case). But in Algol 68 it's just an abbreviation;
the case "statement" is an expression anyway.

-- Richard
--
Please remember to mention me / in tapes you leave behind.

Bartc

unread,
Mar 31, 2009, 6:36:36 PM3/31/09
to

"Richard Tobin" <ric...@cogsci.ed.ac.uk> wrote in message
news:gqtvrb$1b6j$3...@pc-news.cogsci.ed.ac.uk...

> In article <add9f$49d23e67$d5337e4d$10...@cache2.tilbu1.nb.home.nl>,
> Skybuck Flying <Blood...@hotmail.com> wrote:
>
>>Now imagine if it was a case statement operator:
>>
>>A ? 0 : 1 : 2 : 4 : 5
>
> Algol 68 has an abbreviated case statement similar to that:
>
> (A | 0, 1, 2, 4, 5 | -1)
>
> (-1 is the default case). But in Algol 68 it's just an abbreviation;
> the case "statement" is an expression anyway.

Skybuck is assuming A is going to be 0 or 1. In practice the first argument
to ?: could be any integer value, so ?: is not useable as an indexed 2-way
selection, without doing something like !A ? B : C, which looks confusing.

The A-68 construction would be quite a neat addition to C, although A-68 I
think uses 1-based indexing.

--
Bartc

luserXtrog

unread,
Mar 31, 2009, 8:08:37 PM3/31/09
to
On Mar 31, 11:01 am, "Skybuck Flying" <BloodySh...@hotmail.com> wrote:
> It's very easy to explain the logic behind my reasoning:
>
> 1 comes after 0.
>

This is only true in the universe of natural numbers. It is not true
in our universe based on the evidences of palaeography, cosmology,
theology, and love. One must have something before one can know the
lack of it.

> So
>
> A ? 0 : 1
>
> Is the most logical way to view things ;)
>

...unless you relate it to the question mark as it behaves in natural
languages, which it greatly resembles in shape and encoding. N'est-ce
pas? Oui : Non; .

> Now imagine if it was a case statement operator:
>
> A ? 0 : 1 : 2 : 4 : 5
>
> ^ Highly logical.
>
> I rest my case ;)
>

Begging the question.

Assume proposition 1 is true.
The implication "proposition 1 implies proposition 1" is true.
The conclusion "proposition 1 is true" therefore follows.

So what?!

--
leex tool

Skybuck Flying

unread,
Apr 1, 2009, 1:01:49 AM4/1/09
to

"Skybuck Flying" <Blood...@hotmail.com> wrote in message
news:add9f$49d23e67$d5337e4d$10...@cache2.tilbu1.nb.home.nl...

Ohoh, I see I made a mistake, I ment to write:

A ? 0 : 1 : 2 : 3 : 4 : 5

Like a nice incrementing case statement... oh well ;) :)

Bye,
Skybuck.


Skybuck Flying

unread,
Apr 1, 2009, 1:08:13 AM4/1/09
to

"Bartc" <ba...@freeuk.com> wrote in message
news:AOtAl.6659$OO7....@text.news.virginmedia.com...

1. No documentation.
2. Does not compile.

Please provide example within 3 days eitherwise you end up on my ban list.

Bye,
Skybuck.


Richard Tobin

unread,
Apr 1, 2009, 2:08:30 AM4/1/09
to
In article <1da55$49d2f6b2$d5337e4d$56...@cache1.tilbu1.nb.home.nl>,
Skybuck Flying <Blood...@hotmail.com> wrote:

>> You need C's :? operator which does exactly what you want:
>>
>> A : 0 ? 1

>1. No documentation.
>2. Does not compile.

You are a moron.

>Please provide example within 3 days eitherwise you end up on my ban list.

Please add me too.

Richard Heathfield

unread,
Apr 1, 2009, 2:20:29 AM4/1/09
to
Richard Tobin said:

> In article
> <1da55$49d2f6b2$d5337e4d$56...@cache1.tilbu1.nb.home.nl>, Skybuck
> Flying <Blood...@hotmail.com> wrote:
>
>>> You need C's :? operator which does exactly what you want:
>>>
>>> A : 0 ? 1
>
>>1. No documentation.
>>2. Does not compile.
>
> You are a moron.

Unlikely. He is, however, a pain. Killfiling is a realistic option.

>>Please provide example within 3 days eitherwise you end up on my
>>ban list.
>
> Please add me too.

An interesting request, but one that is unlikely to have any useful
effect.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

luserXtrog

unread,
Apr 1, 2009, 3:04:23 AM4/1/09
to
On Apr 1, 12:01 am, "Skybuck Flying" <BloodySh...@hotmail.com> wrote:
> "Skybuck Flying" <BloodySh...@hotmail.com> wrote in message

While perhaps aesthetically balanced upon the line, one wonders of
what possible use such a construct would be. It appears that,
dependant upon the value within the variable A (presuming an int), the
expression returns an integer value coincidentally indentical to the
value retrieved from A itself. There is such a an expression already:

A;

If, on the otherhand, the illustration was intended to illustrate
resolving an integer to a different associated value, one would
normally use an array. C99 allows complex literals permitting
something like this:

#include <stdio.h>
int main(void) {
int A=0;
printf("%d\n", (int []){1,2,3,4,5}[A]);
/* or even */
printf("%d\n", A[(int []){1,2,3,4,5}]);
}

When A is 0, the expression A[(int []){1,2,3,4,5}] resolves to the
first element of the anonymous array, here 1.

--
looks true

luserXtrog

unread,
Apr 1, 2009, 3:28:48 AM4/1/09
to
Even better:

#include <stdio.h>
#define from [(int []){
#define these }]
int main(void) {
    int A=0;
    printf("%d\n", A from 1,2,3,4,5 these );
return (int)0x401F00L;
}

Forgot the return!

--
el XtravaganZ

Skybuck Flying

unread,
Apr 1, 2009, 4:23:56 AM4/1/09
to
Haha good april's fool joke.

Though Bart is still gonna get banned since it wasn't april first yet ;)
unless he provides good example are admits he was mistaken ;)

Bye,
Skybuck.


Skybuck Flying

unread,
Apr 1, 2009, 4:26:20 AM4/1/09
to
The intention ofcourse is to execute different code based on the value of A
like a case statement in pascal or a switch in C.

Examples:

A = 4;

A ? B = 1; C = 2; D = 3; E = 4; F = 5;

A ? Init(); Work(); Draw(); Next(); CleanUp();

Bye,
Skybuck.


Richard Bos

unread,
Apr 1, 2009, 5:19:43 AM4/1/09
to
"Skybuck Flying" <Blood...@hotmail.com> wrote:

> Haha good april's fool joke.

No, you're not.

Richard

pete

unread,
Apr 1, 2009, 6:42:12 AM4/1/09
to

I would write that, this way:

func[A]();


/* BEGIN new.c */

#include <stdio.h>

void Init(void);
void Work(void);
void Draw(void);
void Next(void);
void CleanUp(void);

int main(void)
{
void (*func[])(void) = {Init,Work,Draw,Next,CleanUp};
size_t A;

for (A = 0; A != sizeof func / sizeof *func; ++A) {
func[A]();
}
return 0;
}

void Init(void)
{
puts("Init");
}
void Work(void)
{
puts("Work");
}

void Draw(void)
{
puts("Draw");
}

void Next(void)
{
puts("Next");
}

void CleanUp(void)
{
puts("CleanUp");
}

/* END new.c */

--
pete

Han from China

unread,
Apr 1, 2009, 10:18:52 AM4/1/09
to

I submit that March 31 and April 1 be switched around, since
31 comes after 1.

Yours,
Han from China

--
"Only entropy comes easy." -- Anton Chekhov

luserXtrog

unread,
Apr 1, 2009, 10:48:11 AM4/1/09
to
On Apr 1, 3:26 am, "Skybuck Flying" <BloodySh...@hotmail.com> wrote:
> The intention ofcourse is to execute different code based on the value of A
> like a case statement in pascal or a switch in C.

Both of those constructs already exist.

> Examples:
>
> A = 4;
>
> A ? B = 1; C = 2; D = 3; E = 4; F = 5;
>
> A ? Init(); Work(); Draw(); Next(); CleanUp();
>

This is psychotic.
What, in satan's glorious name, are you driving at?
Statements and Expressions are not the same.

No thanks for the fish, motherfucker?!

--
looza rockstar tribble

Lew Pitcher

unread,
Apr 1, 2009, 11:13:30 AM4/1/09
to
On April 1, 2009 04:26, in comp.lang.c, Skybuck Flying
(Blood...@hotmail.com) wrote:

I suppose that you /could/ use the trinary operator in such a manner, but
that's like using a shoe to drive a nail: it is possible, but the tool
wasn't designed for that task, and there are tools that were specifically
designed for it.

If you are looking to "execute different code based on the value of A", a
switch() statement, or an if() statement, or even a convolution of a loop
and subscript would work much better, and (at least, for two of them)
are /specifically/ designed to do this work.

Kenneth Brody

unread,
Apr 1, 2009, 11:43:23 AM4/1/09
to
Han from China wrote:
> Skybuck Flying wrote:
>> Haha good april's fool joke.
>>
>> Though Bart is still gonna get banned since it wasn't april first yet ;)
>> unless he provides good example are admits he was mistaken ;)
>
> I submit that March 31 and April 1 be switched around, since
> 31 comes after 1.

Sorry, no.

#include <stdio.h>

int main(void)
{
int March31 = 3/31;
int April1 = 4/1;

if ( March31 >= April1 )
printf("April 1 is less than March 31.\n");
else
printf("April 1 is not less than March 31.\n");
}

--
Kenneth Brody

Han from China

unread,
Apr 1, 2009, 12:40:06 PM4/1/09
to
Kenneth Brody wrote:
> int March31 = 3/31;
> int April1 = 4/1;

Implementation-defined date representation.

int March31 = 31/3;
int April1 = 1/4;

will get 'er going.

Flash Gordon

unread,
Apr 1, 2009, 1:01:29 PM4/1/09
to
luserXtrog wrote:
> On Apr 1, 3:26 am, "Skybuck Flying" <BloodySh...@hotmail.com> wrote:

<snip>

> This is psychotic.
> What, in satan's glorious name, are you driving at?

Check the OPs posting history on this group and you will see that it
normally posts complete rubbish. The best thing to do is ignore it.
--
Flash Gordon

luserXtrog

unread,
Apr 1, 2009, 2:30:51 PM4/1/09
to

ACK

luserXtrog

unread,
Apr 1, 2009, 4:10:22 PM4/1/09
to

Nice.
How about:

#include <stdio.h>
int Init(int i) { puts( "Init"); return i+1;}
int Work(int i) { puts( "Work"); return i+1;}
int Draw(int i) { puts( "Draw"); return i+1;}
int Next(int i) { puts( "Next"); return i+1;}
int CleanUp(int i) { puts("CleanUp"); return -1;}
typedef int (*ifi)(int);
int main(void) {
int A = 0;
while((A=A[(ifi[]){Init,Work,Draw,Next,CleanUp}](A))!=-1)0xF00L;
return 0;
}

--
lxt

0 new messages