Subject: a comment is a comment is a comment?

From: Muli Ben-Yehuda <[email-masked]>
Date: Tue, 8 Jul 2003 22:22:27 +0300

"Write[0] a C[1] program that will contain a comment which is not
closed, which will compile cleanly, and which will give the wrong
answer because the comment is not closed".  

[0] I was asked this question as an extra credit question in a class
I'm taking, Algorithmics. Obviously, I won't be using any of the
solutions (if any) mentioned here. But I've been butting my head
against the wall for the last three hours, and I'm fairly convinced it
can't be solved given a strict interpretation of the rules. 

[1] The original phrasing was "in either of C, C++, java or pascal". 
-- 
Muli Ben-Yehuda
http://www.mulix.org
http://www.livejournal.com/~mulix/

---

From: Alon Altman <[email-masked]>
Date: Tue, 8 Jul 2003 22:44:15 +0300 (IDT)

> [1] The original phrasing was "in either of C, C++, java or pascal".

int main() {
  int i=0; /* Counter for the loop
  for (i=0; i<10; i++) /* The main loop */
    printf("%d\n",i);
}

It's harder with pascal, as it supports nested comments.

-- 
This message was sent by Alon Altman ([email-masked]) ICQ:1366540
GPG public key at http://alon.wox.org/pubkey.txt
Key fingerprint = A670 6C81 19D3 3773 3627  DE14 B44A 50A3 FE06 7F24
--------------------------------------------------------------------------
 -=[ Random Fortune ]=-
The little pieces of my life I give to you, with love, to make a quilt
to keep away the cold.

---

From: Muli Ben-Yehuda <[email-masked]>
Date: Tue, 8 Jul 2003 22:52:33 +0300

>   for (i=0; i<10; i++) /* The main loop */

From a language point of view, the code above has *one* comment from
the first '/*' to the last '*/'.  You are correct that from a "human"
point of view, it fullfils the requirements (it has what *appears* to
be an unclosed comment), but from a language point of view, it
doesn't (and I don't think any solution is possible). Nonetheless,
nice solution! 
-- 
Muli Ben-Yehuda
http://www.mulix.org
http://www.livejournal.com/~mulix/

---

From: Oleg Goldshmidt <[email-masked]>
Date: 08 Jul 2003 23:21:30 +0300

> [1] The original phrasing was "in either of C, C++, java or pascal". 

Well, you cannot use this because it is lifted without permission
straight from Herb Sutter's "Guru of the Week"[1]:

#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;
}

It does answer the original formulation (that included C++). I have
no idea what its relation to a course in Algorithmics is.

[1] Those who have scratched their heads long enough can peep into
    http://www.gotw.ca/gotw/086.htm for the full story

-- 
Oleg Goldshmidt | [email-masked]

---

From: guy keren <[email-masked]>
Date: Wed, 9 Jul 2003 12:49:42 +0300 (EET DST)

> no idea what its relation to a course in Algorithmics is.

what does this code got to do with a non-closed comment? the 'bug' above
is the extra semicollon in the 'for' line. how does this relate to the
'non-closed comment' muli mentioned?

-- 
guy

"For world domination - press 1,
 or dial 0, and please hold, for the creator." -- nob o. dy

---

From: Oleg Goldshmidt <[email-masked]>
Date: 09 Jul 2003 13:18:11 +0300

> 'non-closed comment' muli mentioned?

Short answer: no, the extra semicolon is not the bug. 

Longer answer: the bug was caused by someone ending a comment with
multiple question marks and accidentally taking a finger off the shift
key.

Full answer: go to the URL in my original posting.