Q:

Variables not being destroyed with each iteration of a while loop?

#include <stdio.h>
#define IN 1
#define OUT 0

int main()
{
  while (1)
  {
    FILE *fptr1;
    char filename[100];
    printf("Enter the filename to open for reading \n");
    scanf("%s", filename); 
    fptr1 = fopen(filename, "r");

    int c, nl, nw, nc, state; // These variables aren't being reset.

    while ((c = fgetc(fptr1)) != EOF) 
    {
      ++nc;
      if (c == '\n') ++nl;
      if (c == '\t' || c == '\n' || c == ' ') state = OUT;
      else if (state == OUT)
      {
        state = IN;
        ++nw;
      }
    };
    printf("Lines: %d Words: %d Characters: %d\n", nl, nw, nc);
  }
  return 0;
}

This is a simple program to parse a file and output how many lines, words, and characters it has. nl, nw, nc are incremented when new lines, words, and characters are detected. The output:

Enter the filename to open for reading
data.txt
Lines: 6 Words: 15 Characters: 81 
Enter the filename to open for reading 
data.txt 
Lines: 12 Words: 30 Characters: 162 
Enter the filename to open for reading

It works correctly the first time, but it doesn't destroy nl, nw, nc when the while loop finishes and just keeps adding to them. Why?

A:

You are invoking undefined behavior by accessing the unintialized variable.
And at each iteration their a new set of variables you get. That means you can't rely on the variable values from the previous iteration.
int c, nl = 0, nw = 0, nc = 0, state = OUT;

Few other notes change scanf to scanf("%99s",filename);.
Also close the file once you are done working with it. Otherwise if you use too many files without closing them you may run out of resources. 
flocse(fptr1); 
In this case even if you don't close it it will be closed upon exiting the process but still it is a good habit to deallocate resources that you use. It's good practice.

