# Ternary Operator ? :

• (2 Pages)
• 1
• 2

## 21 Replies - 3728 Views - Last Post: 31 May 2009 - 10:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=107560&amp;s=7ce5c1542e24aa5bfc80ecc572088175&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 IngeniousHax

• |>|20-514<|{3|2

Reputation: 83
• Posts: 1,377
• Joined: 28-March 09

## Re: Ternary Operator ? :

Posted 31 May 2009 - 09:36 PM

NickDMax, on 31 May, 2009 - 08:32 PM, said:

Anyone who puts x++ + ++x in real code should be shot even if the language allows it.

I completely agree...It's ludicrous to do such a thing.

### #17 Dantheman

• D.I.C Regular

Reputation: 34
• Posts: 445
• Joined: 27-May 09

## Re: Ternary Operator ? :

Posted 31 May 2009 - 09:42 PM

IngeniousHax, on 31 May, 2009 - 08:36 PM, said:

NickDMax, on 31 May, 2009 - 08:32 PM, said:

Anyone who puts x++ + ++x in real code should be shot even if the language allows it.

I completely agree...It's ludicrous to do such a thing.

Not if you're writing obfuscated code.

Guess the output of the following code without compiling it.

```int main() {
int x = 0;
++x+=x++ + ++x;
cout << x;
return 0;
}

```

This post has been edited by Dantheman: 31 May 2009 - 09:48 PM

### #18 IngeniousHax

• |>|20-514<|{3|2

Reputation: 83
• Posts: 1,377
• Joined: 28-March 09

## Re: Ternary Operator ? :

Posted 31 May 2009 - 09:45 PM

Yeah, but that's a whole different scenario.

I believe the answer would be 1

EDIT:: After guessing, I went to test, and it won't compile, technically the answer is NOP

This post has been edited by IngeniousHax: 31 May 2009 - 09:48 PM

### #19 Dantheman

• D.I.C Regular

Reputation: 34
• Posts: 445
• Joined: 27-May 09

## Re: Ternary Operator ? :

Posted 31 May 2009 - 09:52 PM

Yea, I went crazy with pluses there. Try the fixed version. I was pretty shocked to see the answer.

### #20 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Ternary Operator ? :

Posted 31 May 2009 - 10:07 PM

Well... If you are writing obfucated code more power to you. Don't forget the comma operator and other alternate syntaxes. Heck whip out some trigraphs.

### #21 jcmaster2

Reputation: 2
• Posts: 183
• Joined: 27-April 09

## Re: Ternary Operator ? :

Posted 31 May 2009 - 10:22 PM

Well since it looks like cpp

```int main() {
int x = 0;
++x+=x++ + ++x;
cout << x;
return 0;
}

```

Quote

After guessing, I went to test, and it won't compile, technically the answer is NOP

### #22 IngeniousHax

• |>|20-514<|{3|2

Reputation: 83
• Posts: 1,377
• Joined: 28-March 09

## Re: Ternary Operator ? :

Posted 31 May 2009 - 10:49 PM

It prints...Nothing. Absolutely nothing...How bizarre...You would think because of all the ++'s atleast a 1 would be formed...

I added a for loop and this is the output I got...Interesting::

The i value is: 0 and the x value is: 7
The i value is: 1 and the x value is: 28
The i value is: 2 and the x value is: 91
The i value is: 3 and the x value is: 280
The i value is: 4 and the x value is: 847
The i value is: 5 and the x value is: 2548
The i value is: 6 and the x value is: 7651
The i value is: 7 and the x value is: 22960
The i value is: 8 and the x value is: 68887
The i value is: 9 and the x value is: 206668

```#include <iostream>
using namespace std;

int main() {
int x = 0;
for(int i = 0; i < 10; i++){
++x+=x++ + ++x;
printf("The i value is: %d and the x value is: %d\n", i, x);
}
return 0;
}

```

This post has been edited by IngeniousHax: 31 May 2009 - 10:51 PM