0
Can someone explain to me the codes?
#include <stdio.h> #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ /* count lines, words, and characters in input */ main() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == '\n') ++nl; if (c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf("%d %d %d\n", nl, nw, nc); } Why using IN and OUT? What is the purpose of doing that? Thank you.
2 Answers
+ 3
#define IN 1
#define OUT 0
are called preprocessors in C which are nothing but constants that you use in your programm. Every occurrence of IN and OUT is replaced with their respective values 1 and 0 during the preprocessing phase which occurs before the compilation process.
0
I'd say it's to make it a bit more readable. state = OUT is understandable, but state = 0 is not really.