Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ike Naar <ike@iceland.freeshell.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: program which removes comments from C source |
| Date | 2011-06-14 07:23 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrn3vfsive32n.ndj.ike@iceland.freeshell.org> (permalink) |
| References | <a6268caf-18a8-4e8f-9b2e-433fc57397a8@j31g2000yqe.googlegroups.com> <1b880733-0be2-4a3e-a90e-6bd4ef28cf43@q30g2000yqb.googlegroups.com> |
On 2011-06-14, Ceriousmall <divadsmall@gmail.com> wrote:
> int gotline(char line[], int max)
> {
> int c, length;
>
> for (length = 0; length < max-2 && (c = getchar()) != EOF && c !=
> '\n'; ++length)
> line[length] = c;
>
> if (c == '\n')
> line[length++] = c;
>
> line[length] = '\0';
>
> if (c == EOF)
> return c;
> }
The gotline() function does not always return a value.
What is the purpose of the ``if (c == EOF)'' test?
> [snip]
> int main(void)
> {
> char line[MAXLINE+1];
> int at_start, at_end, return_val;
>
> at_start = cmt_state = qts_state = OUT;
>
> while (at_start != EOF) {
> return_val = gotline(line, MAXLINE);
> at_end = return_val == EOF;
> delcom(line, IN, OUT);
> printf("%s", line);
>
> if (at_end)
> at_start = return_val;
> }
> return 0;
> }
The last element of line[] is never used; it could be declared as
line[MAXLINE];
at_end and return_val are redundant; the following does the same thing:
int main(void)
{
char line[MAXLINE+1];
int at_start;
at_start = cmt_state = qts_state = OUT;
while (at_start != EOF) {
at_start = gotline(line, MAXLINE);
delcom(line, IN, OUT);
printf("%s", line);
}
return 0;
}
The program does not perform well if there are lines longer than MAXLINE
(you can verify this by setting MAXLINE to a small value, e.g. 32, and
let the program process itself as input).
Why do you process the input line-by-line anyway? The program only needs
to buffer a few (probably two) characters, not an entire line; comment
text can be replaced by spaces on-the-fly, no need to postpone this
until end-of-line or end-of-comment is seen.
The line handling makes the program more complicated than necessary.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
program which removes comments from C source Ceriousmall <divadsmall@gmail.com> - 2011-06-13 18:20 -0700
Re: program which removes comments from C source Ceriousmall <divadsmall@gmail.com> - 2011-06-13 18:23 -0700
Re: program which removes comments from C source Keith Thompson <kst-u@mib.org> - 2011-06-13 19:23 -0700
Re: program which removes comments from C source Ceriousmall <divadsmall@gmail.com> - 2011-06-13 19:37 -0700
Re: program which removes comments from C source Ceriousmall <divadsmall@gmail.com> - 2011-06-13 19:41 -0700
Re: program which removes comments from C source Ian Collins <ian-news@hotmail.com> - 2011-06-14 14:57 +1200
Re: program which removes comments from C source Ceriousmall <divadsmall@gmail.com> - 2011-06-13 19:53 -0700
Re: program which removes comments from C source Ike Naar <ike@iceland.freeshell.org> - 2011-06-14 07:23 +0000
Re: program which removes comments from C source Ceriousmall <divadsmall@gmail.com> - 2011-06-14 14:15 -0700
Re: program which removes comments from C source Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-14 16:30 -0700
csiph-web