[C] remove comments

This is a discussion on [C] remove comments within the C Programming forums, part of the General Programming Boards category; He said that he wasn't including C99 single line comments, only block comments. && has higher priority than ||, so ...

  1. #46
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    He said that he wasn't including C99 single line comments, only block comments.

    && has higher priority than ||, so if you don't remove it, then you need to put paren around the all the or statements to make it work right.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #47
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Quote Originally Posted by Aksel View Post
    It has improved a lot. It does not handle this contrived situation:

    Code:
    // Look, I am being clever, using line-continuation to turn a single-line comment\
    into a multi-line comment.
    GCC will warn you about such multi-line comments if you use "-Wall".

    If you use -Wall, GCC will also warn you about

    Code:
        if(state == normal || 
        state == squote || state == squote2 || state == squote3 || state == squote4 || state == squote_escape ||
        state == dquote || state == dquote_escape || state == octal || state == hexa && 
        state!=comment_exit2)
    I think it is safe to remove the last part "&& state != comment_exit" which is a given.
    Whats -Wall used for? Why?

    I tried writing it into parameters of my compilers, it still doesnt recognizes the case with comments you mentioned.
    Last edited by Tool; 11-22-2009 at 07:24 AM.

  3. #48
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Quote Originally Posted by Tool View Post
    Whats -Wall used for? Why?
    Warning Options - Using the GNU Compiler Collection (GCC)

    This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros. This also enables some language-specific warnings described in C++ Dialect Options and Objective-C and Objective-C++ Dialect Options.
    and

    Note that some warning flags are not implied by -Wall. Some of them warn about constructions that users generally do not consider questionable, but which occasionally you might wish to check for; others warn about constructions that are necessary or hard to avoid in some cases, and there is no simple way to modify the code to suppress the warning. Some of them are enabled by -Wextra but many of them must be enabled individually.
    [edit]
    Using -Wall will often draw my attention to potential bugs.

    I tried writing it into parameters of my compilers, it still doesnt recognizes the case with comments you mentioned.
    "-Wall" is specific to GCC, your compiler may have a similar option though. Which compiler is it?
    Last edited by Aksel; 11-22-2009 at 07:32 AM.

  4. #49
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Anyways, i shortened the code alot, the only reason i had squote states so much last time was the syntax checker from which i took most of the code. It dealt with the states aswell, just much more detail.
    So here it is, the most shortened version:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        
         int c;
         enum states { normal,
                       squote, squote_escape,
                       comment_entry, comment, comment_exit, comment_exit2, comment2,
                       dquote, dquote_escape };
         int state = normal; 
         int second; /* stores the tmp character after slash */
         int flag=0; /* if 1, puts the character stored in second */
         
      
    
           while((c=getchar())!=EOF)
          {
             if(state == normal && c=='\'') { state = squote; }
             else if(state == normal && c=='"') { state = dquote; }
             else if(state == normal && c=='/')
             { 
                  second=getchar(); 
                  if(second=='*') 
                  {
                     ungetc(c, stdin); 
                     state = comment; 
                  }
                  
                  else if(second=='/') 
                  {
                     ungetc(c, stdin);
                     state = comment2; 
                  }
                  
                  else if(second=='\'') {state = squote; }
                  else if(second=='"') { state = dquote; }
                  
                  else flag=1;
             }    
          
             else if(state == comment && c=='*') { state = comment_exit; }
             else if(state == comment) {}
             
             else if(state == comment_exit && c=='*') {}
             else if(state == comment_exit && c=='/') { putchar(' '); state = comment_exit2; }
             else if(state == comment_exit) { state = comment; }
             
             else if(state == comment2 && c=='\n') { state = normal; }
             else if(state == comment2) { }         
             
             else if(state == squote && c=='\\') { state = squote_escape; }
             else if(state == squote && (c=='\'' || c=='\n')) { state = normal; }
             else if(state == squote) { }
             
             else if(state == squote_escape && c=='\n') { state = normal; }
             else if(state == squote_escape) { state = squote; }
             
             else if(state == dquote && c=='\n') { state = normal; }
             else if(state == dquote && c=='"') { state = normal; }
             else if(state == dquote && c=='\\') { state = dquote_escape; }
             else if(state == dquote) { }
             
             else if(state ==dquote_escape) { state = dquote; }
             
             if((state == normal || state == squote || state == squote_escape || state == dquote || state == dquote_escape) && state!=comment_exit2) 
             putchar(c); 
             
             if(state==comment_exit2) { state = normal; }     
             if(flag==1) { flag=0; putchar(second); }
             
          }
         
       
        printf("Press any key to continue");   
        getchar();	
        return 0;
    }
    Last edited by Tool; 11-22-2009 at 07:39 AM.

  5. #50
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Quote Originally Posted by Aksel View Post
    Warning Options - Using the GNU Compiler Collection (GCC)



    and



    [edit]
    Using -Wall will often draw my attention to potential bugs.



    "-Wall" is specific to GCC, your compiler may have a similar option though. Which compiler is it?
    Dev-C++.
    /ashamed

  6. #51
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    Thumbs up very easy in c. use commandline arguments in the place filename...

    insert
    Code:
    /*  comment line */
    //comment line
    /*
    comment line
    */
    #include"stdio.h"
    
    void main(int argc,char *argv[])
    {
    	char ch,ch1,ch2;
    	FILE *fp,*fp1;
    	fp=fopen("filename","r");/*in place of "filename" u can put a command line argument file argv[1]*/ 
    	fp1=fopen("temp","w");
    	if(!fp || !fp1)
    	{
    		printf("cannot open file ");
    		exit(0);
    	}
    
    
    	while((ch=getc(fp)) != EOF)
    	{
    		
    		if(ch=='/')
    			if( ((ch1=getc(fp))!= EOF) && (ch1=='*') )
    				do{
    					if(ch1=='*' && ((ch2=getc(fp))!= EOF) && ch2=='/') 
    						break;
    					continue;
    				}while((ch1=getc(fp))!= EOF);
    			else
    				if(ch1=='/')
    					do{
    						if( ((ch2=getc(fp)) != EOF) && ch2=='\n')
    							break;
    						continue;
    					}while((ch1=getc(fp))!= EOF);
    				else
    					{
    						putc(ch,fp1);
    						putc(ch1,fp1);
    					}
    		else
    			putc(ch,fp1);
    
    	}
    	
    	
    	fclose(fp);
    	fclose(fp1);
    	remove("filename");
    	rename("temp","filename");
    	
    }
    Last edited by c's mad; 11-29-2009 at 05:01 AM.

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove comments
    By St0rM-MaN in forum C Programming
    Replies: 4
    Last Post: 05-19-2007, 12:03 AM
  2. program to remove comments from source
    By Abda92 in forum C Programming
    Replies: 12
    Last Post: 12-25-2006, 05:18 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. The Art of Writing Comments :: Software Engineering
    By kuphryn in forum C++ Programming
    Replies: 15
    Last Post: 11-23-2002, 05:18 PM
  5. remove comments from source code
    By limbo100 in forum C Programming
    Replies: 2
    Last Post: 09-29-2001, 07:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21