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.

8th Mar 2020, 5:14 AM
Woody Lin
Woody Lin - avatar
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.
8th Mar 2020, 6:13 AM
Avinesh
Avinesh - avatar
0
I'd say it's to make it a bit more readable. state = OUT is understandable, but state = 0 is not really.
8th Mar 2020, 5:45 AM
Kevin Liu
Kevin Liu - avatar