I'm working through K&R C Programming Language, and wrote a program to remove comments from a C program. The other exercises from the book have been challenging, but I've done them all so far.
I compile it with no complaints, and run it, but my shell comes back with no visible sign of anything. (I am on NT working from the command line with gcc). I add
, and then get complaints that my variables (appearing right below my printf) are not initialized. Without the printf, the compiler is silent. I am compling the right program; the variables it mentions when I have the printf statement are right. Here's the program.
I compile it with no complaints, and run it, but my shell comes back with no visible sign of anything. (I am on NT working from the command line with gcc). I add
Code:
printf("in main");
Code:
/*remcoms.c*
* removes c comments from a program
*/
#include <stdio.h>
#define BUFFSIZE 1000
int getline(char line[], int maxline);
int endcomm(char s[], int ind);
int startcomm(char s[], int ind);
int main(){
char line [BUFFSIZE]; /*holds input line*/
int incomment =0; /* true within a comment*/
int len;
int i;
while ( (len=getline(line, BUFFSIZE) ) ){
while(i<len){
/* echo input until the start of a comment*/
while (!startcomm(line, i) && incomment ==0){
putchar(line[i]);
i++;
}
/* stop echoing input when a comment is reached */
if(startcomm(line, i)){
incomment = 1;
while( !endcomm(line,i) )
/*move past the comment*/
i++;
}
/* when we reach the end of a comment, we need to
* move past the two characters '*' and '\' */
if (endcomm(line, i) ){
i++;
i++;
incomment =0;
}
}
}
return 0;
}
/*
* K&R fxn, retrieves a line of input
* into the char array s, and returns
* the length of the line
*/
int getline(char s[], int lim){
int c,i;
for(i=0; i< lim-1 &&(c = getchar())!= EOF && c !='\n'; ++i)
s[i] = c;
if( c== '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int startcomm(char s[], int ind){
return (s[ind] == '/' && s[ind+1] =='*');
}
int endcomm(char s[], int ind){
return (s[ind] == '*' && s[ind +1] == '/');
}