I found This program is K&R The C programming book I’m new to C programming
I’m not getting why the variable d is used and the value of d is change to d=0
then d=1 between program.
Can anyone explain me what this program logic does I know this program is to replace
tabs with \t likewise.
#include <stdio.h>
int main()
{
int c, d;
while ( (c=getchar()) != EOF) {
d = 0;
if (c == '\\') {
putchar('\\');
putchar('\\');
d = 1;
}
if (c == '\t') {
putchar('\\');
putchar('t');
d = 1;
}
if (c == '\b') {
putchar('\\');
putchar('b');
d = 1;
}
if (d == 0)
putchar(c);
}
return 0;
}
dis used as a flag variable (i.e. a variable that communicates a single binary condition, yes or no). The program setsdto one when it has interpreted the charactercas a special character, and printed its slash-prefixed value. The program checks thatdflag later on to determine if it has printedcor not. If the answer is no, then the program printsc; otherwise, it continues to the next iteration of the loop.