Ternary Operator ? :

Bad practice?

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2955 Views - Last Post: 31 May 2009 - 10:49 PM Rate Topic: -----

#16 IngeniousHax  Icon User is offline

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

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Ternary Operator ? :

Posted 31 May 2009 - 09:36 PM

View PostNickDMax, 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.
Was This Post Helpful? 0
  • +
  • -

#17 Dantheman  Icon User is offline

  • D.I.C Regular

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

Re: Ternary Operator ? :

Posted 31 May 2009 - 09:42 PM

View PostIngeniousHax, on 31 May, 2009 - 08:36 PM, said:

View PostNickDMax, 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

Was This Post Helpful? 0
  • +
  • -

#18 IngeniousHax  Icon User is offline

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

Reputation: 78
  • View blog
  • Posts: 1,358
  • 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

Was This Post Helpful? 0
  • +
  • -

#19 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#20 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#21 jcmaster2  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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



Undefined in C/C++- technical answer B)
Was This Post Helpful? 0
  • +
  • -

#22 IngeniousHax  Icon User is offline

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

Reputation: 78
  • View blog
  • Posts: 1,358
  • 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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2