Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to remove comments from C program in C

1 Answer

0 votes
#include <stdio.h>

int main(void) {
	FILE *fpr = fopen ("file.c", "r");
	FILE *fprw = fopen ("new_file.c", "w");
	if (fpr == NULL) {
		printf("Error open file.c");
		return 1;
	}
	if (fprw == NULL) {
		printf("Error open new_file.c");
		return 1;
	}
	char ch, nextch;
	
	nextch = fgetc(fpr);
	while (nextch != EOF) {
		ch = nextch;
		nextch = fgetc(fpr);

		if ((ch == '/') && (nextch == '*')) {
			ch = fgetc(fpr);
			nextch = fgetc(fpr);
			while (!((ch == '*') && (nextch == '/'))) {
				ch = nextch;
				nextch = fgetc(fpr);
			}
			nextch = fgetc(fpr);
			continue;
		 } else if ((ch=='/') && (nextch == '/')) {
                    nextch = fgetc(fpr);
					while (!(nextch == '\n')) {
						nextch = fgetc(fpr);
                 }
         nextch = fgetc(fpr);
         continue;
     }

		putc(ch, fprw);
	}
	fclose (fpr);
	fclose (fprw);
	  
	return 0;
}

  
/*

file.c
------

// #include <stdio.h>
//
// #define ROW 5
// #define COL 10
// 
// void func(char *parr, int row, int col) {
//     /*for (int i = 0; i < row; i++) {
//         printf("%s\n", parr + sizeof(char) * col * i);
//     }*/
// }
// 
// int main(void) {
//     char arr[ROW][COL] = {"c", "c++", "c#", "java", "python"};
// 
//     /* func((char*)arr, ROW, COL); */
// 	
// 	   // one line comment
// 
//     return 0;
// }
// 
//    
//    
// /*
// run:
//    
// c
// c++
// c#
// java
// python
//    
// */


 
   
/*
run:
   
#include <stdio.h>

#define ROW 5
#define COL 10

void func(char *parr, int row, int col) {
    
}

int main(void) {
    char arr[ROW][COL] = {"c", "c++", "c#", "java", "python"};

    
	
	
    return 0;
}
   
*/

 

 



answered Jun 23, 2020 by avibootz

Related questions

1 answer 140 views
1 answer 235 views
1 answer 233 views
233 views asked Jun 11, 2015 by avibootz
1 answer 100 views
1 answer 167 views
167 views asked Dec 25, 2021 by avibootz
1 answer 158 views
158 views asked Dec 25, 2021 by avibootz
1 answer 115 views
115 views asked Jun 25, 2021 by avibootz
...