Lucas Mezalira
Lucas Mezalira

Reputation: 183

Why is this program yielding wrong output

This program is supposed to remove all comments from a C source code (in this case comments are considered double slashes '//' and a newline character '\n' and anything in between them, and also anything between '/* ' and '*/'.

The program:

#include <stdio.h>

/* This is a multi line comment
testing */

int main() {
int c;

while ((c = getchar()) != EOF)
{
    if (c == '/') //Possible comment
    {
        c = getchar();
        if (c == '/') // Single line comment
            while (c = getchar()) //While there is a character and is not EOF
                if (c == '\n') //If a space character is found, end of comment reached, end loop
                    break; 


        else if (c == '*') //Multi line comment 
        {
            while (c = getchar()) //While there is a character and it is not EOF
            {   
                if (c == '*' && getchar() == '/') //If c equals '*' and the next character equals '/', end of comment reached, end loop
                    break;
            }                   
        }

        else putchar('/'); putchar(c); //If not comment, print '/' and the character next to it
    }

    else putchar(c); //if not comment, print character
}

}

After I use this source code as its own input, this is the output I get:

#include <stdio.h>

* This is a multi line comment
testing *

int main() {
int c;

while ((c = getchar()) != EOF)
{
    if (c == '') ////////////////
    {
        c = getchar();
        if (c == '') ////////////////////
            while (c = getchar()) /////////////////////////////////////////
                if (c == '\n') ///////////////////////////////////////////////////////////////
                    break; 


        else if (c == '*') ///////////////////
        {
            while (c = getchar()) ////////////////////////////////////////////
            {   

No more beyond this point. I'm compiling it using g++ on the ubuntu terminal.

As you can see, multi lines comments had only their '/' characters removed, while single line ones, had all their characters replaced by '/'. Apart from that, any '/' characters that were NOT the beginning of a new comment were also removed, as in the line if (c == ''), which was supposed to be if (c == '/').

Does anybody know why? thanks.

Upvotes: 2

Views: 83

Answers (3)

Fiddling Bits
Fiddling Bits

Reputation: 8861

You have some (apparent) logic errors...

1.

while (c = getchar()) //While there is a character and is not EOF

You're assuming that EOF == 0. Why not be explicit and change the preceding line to:

while((c = getchar()) != EOF)

2.

else putchar('/'); putchar(c);

Are both of the putchars supposed to be part of the else clause? If so, you need braces {} around the two putchar statements. Also, give each putchar its own line; it not only looks nicer but it's more readable.

Conclusion

Other than what I've mentioned, your logic looks sound.

Upvotes: 1

hlovdal
hlovdal

Reputation: 28258

As already mentioned, the if/else matching is incorrect. One aditional missing functionality is that you must make it more stateful to keep track of whether you are inside a string or not, e.g.

printf("This is not // a comment\n");

Upvotes: 0

rici
rici

Reputation: 241901

C does not take notice of the way you indent your code. It only cares about its own grammar.

Look carefully at your elses and think about which if they attach to (hint: the closest open one).

There are other bugs, as well. EOF is not 0, so only the first while is correct. And what happens if the comment looks like this: /* something **/?

Upvotes: 2

Related Questions