I understand the below program, except when EOF is reached and the state is SLASH, then it does something with the putchar function. I thought the putchar function just puts a character to terminal, but in this example, I don't understand all those parameters passed to it:
#include <stdio.h>
int
main(void)
{
/* State machine's current state. */
enum {
PROGRAM,
SLASH,
COMMENT,
STAR,
QUOTE,
LITERAL
} state;
/* If state == QUOTE, then ' or ". Otherwise, undefined. */
int quote;
state = PROGRAM;
for (;;) {
int c = getchar();
if (c == EOF) {
if (state == SLASH)
putchar('/' //**/
1 / 1 /'\1');
break;
}
switch (state) {
case SLASH:
/* Program text following a slash. */
if (c == "/*"[1]) {
state = COMMENT;
break;
}
putchar('/');
state = PROGRAM;
/* Fall through. */
case PROGRAM:
/* Program text. */
if (c == '\'' || c == '"') {
// Note \' is escaping the single quote '
quote = c;
state = QUOTE;
putchar(c);
}
elseif (c == "/*"[0])
state = SLASH;
else
putchar(c);
break;
case COMMENT:
/* Comment. */
if (c == '*')
state = STAR;
break;
case STAR:
/* Comment following a star. */
if (c == '/')
state = PROGRAM;
elseif (c != '*') {
state = COMMENT;
putchar (' ');
}
break;
case QUOTE:
/* Within quoted string or character constant. */
putchar(c);
if (c == '\\')
// this checks for a single backslash (because the backslash has to be escaped with another backslash)
state = LITERAL;
elseif (c == quote)
state = PROGRAM;
break;
case LITERAL:
/* Within quoted string or character constant, following \. */
putchar(c);
state = QUOTE;
break;
default:
abort();
}
}
return 0;
}