This is about the comment deletion program in Kernighan and Ritchie (number 1-23 p 34 ANSI eidtion). The following is my solution.
The program works fine with most C programs, deleting comments. However, in some programs with comments that end with multiple asterisks (**/), it poses problems (does not read through). One such example is this one :
However, it works with this program, despite the fact that it has a similar comment at the beginning:
So what do you think is causing the comment deletion program to behave this way?
When you hit a
*in comment state, you read the next character to see if it’s a/. This consumes that character, so it won’t be checked on the next iteration.So:
sees the
*at 1, reads the*at 2, stays in comment mode, andcontinues with the/. Whereas:sees the
*at 2, reads the*at 2, continues, then reads the*at 3 and finds a/following and drops out of comment mode.