Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete all /* */ comments from a C source file?

I have a C file which I copied from somewhere else, but it has a lot of comments like below:

int matrix[20];
/* generate data */
for (index = 0 ;index < 20; index++)
matrix[index] = index + 1;
/* print original data */
for (index = 0; index < 5 ;index++)

How can I delete all the comments enclosed by /* and */. Sometimes, the comments are consist of 4-5 lines, and i need to delete all those lines.

Basically, I need to delete all text between /* and */ and even \n can come in between. Please help me do this using one of sed, awk or perl.

like image 316
Vijay Avatar asked Nov 11 '09 11:11

Vijay


People also ask

How do I remove all comments from code?

📝 Usage. Once installed, remove comments in your code by opening the command palette ( Ctrl + Shift + P ), entering "Remove all comments" and pressing enter. Voilà, all the comments are gone 🎉 !


1 Answers

Why not just use the c preprocessor to do this? Why are you confining yourself to a home-grown regex?

[Edit] This approach also handles Barts printf(".../*...") scenario cleanly

Example:

[File: t.c]
/* This is a comment */
int main () {
    /* 
     * This
     * is 
     * a
     * multiline
     * comment
     */
    int f = 42;
    /*
     * More comments
     */
    return 0;
}

.

$ cpp -P t.c
int main () {







    int f = 42;



    return 0;
}

Or you can remove the whitespace and condense everything

$ cpp -P t.c | egrep -v "^[ \t]*$"
int main () {
    int f = 42;
    return 0;
}

No use re-inventing the wheel, is there?

[Edit] If you want to not expand included files and macroa by this approach, cpp provides flags for this. Consider:

[File: t.c]

#include <stdio.h>
int main () {
    int f = 42;
    printf("   /*  ");
    printf("   */  ");
    return 0;
}

.

$ cpp -P -fpreprocessed t.c | grep -v "^[ \t]*$"
#include <stdio.h>
int main () {
    int f = 42;
    printf("   /*  ");
    printf("   */  ");
    return 0;
}

There is a slight caveat in that macro expansion can be avoided, but the original definition of the macro is stripped from the source.

like image 152
ezpz Avatar answered Oct 04 '22 19:10

ezpz