   Quiz: a couple of interesting problems
  Problem 1:What is the output of the following program on a standards-conforming C++ compiler?#include <iostream>#include <iomanip>int main(){  int x = 1;  for( int i = 0; i < 100; ++i );    // What will the next line do? Increment???????????/    ++x;  std::cout << x << std::endl;}Problem 2:How many distinct errors should be reported when compiling the following code on a conforming C++ compiler?struct X {  static bool f( int* p )  {    return p && 0[p] and not p[1:>>p[2];  };};Problem 3:I have 2 ints i1 and i2.  I want to swap their values without touching anything else.  Only i1 and i2 can be used.I will put up the sources and answers of the first two problems once I see some answers.  I forgot where I saw the third problem though that one is probably rather common.[edited by - Drath on February 5, 2004 11:32:17 AM]
  Problem 1: 2id imagine most compilers would give a warning about that loop not actually doing anything. ;  I am afraid that is incorrect.  Something else wierd is going on there. ;  1. will print 2.Reason: loop doesn''t do anything because of semi-colon at end.Note: you could have made this even harder by changing the comment to:// what will the next line do? Increment???????????\  in which case the last character would be interpreted as a continuation character and the line ++x; would become part of the comment.  This might have been your intention.2. Not 100% sure of this, but no problems?Reason: 0[p] is valid syntax and equivalent to p[0].  ''and'' and ''not'' are valid keywords equivalent to && (or &?) and ! and :> is some funny sequence that interprets as ] (think this was for people working on computers with keyboards that didn''t have a '']'' key).3. i1 ^= i2;i2 ^= i1;i1 ^= i2;  Do I get a prize?Enigma ;  I believe you would use an xor for the 3rd question. I can''t quite remember off the top of my head but I could look it up. ;  // EnigmaBah!  I only got the second one right.  I never knew about ?/ converting to \.  I already knew about :> because I put that in code by accident yet my source somehow compiled.  Working through it I found out it converted to a ].  Does anyone know what other sequences convert to characters?Try Herb Sutter''s http://gotw.ca/ site for some other good problems.  It was the source of the first 2 problems. ;  And of course your prize?A nice big cookie. ;  Ah, I''d forgotten that ''??/'' was equivalent to ''\''.  As to GOTW - how do you think I knew the answers ?Enigma ;  Digraphs:<:  [:> ]<% {%> }%: #%:%: ##"??/" is "\"? That''s neat.Oh and don''t do thisvector<::MyClass> v; ;  Then there is of course many things that are in a way related to this.list> and all related can be a silent and deadly killer when you are using #define 