title: Global file pointer declaration in C
body: <p>There's some problem with the global file pointer declaration, as a result of which I'm getting a segmentation fault. GDB showed that it occurs specifically at the <code>fscanf</code> line in the <code>sourceToStream</code> function. Any help would be appreciated.</p>
  
  <pre><code>#define SOURCE_CODE "source_code.txt"           
  #define CHARACTER_STREAM "character_stream.txt"
  
  static FILE* in_fp;
  static FILE* out_fp;
  
  void sourceToStream() {
      char buf;
  
      while(fscanf(in_fp, "%c", &amp;buf) != EOF) {
          if (buf == '\%')
              while(buf!='\n' &amp;&amp; buf !='\r' &amp;&amp; buf!=EOF)
                  fscanf(in_fp, "%c", &amp;buf);
  
          if(buf != '\n' &amp;&amp; buf != '\r' &amp;&amp; buf != '\t' &amp;&amp; buf != ' ')
              fprintf(out_fp, "%c", buf);
      }
  }
  
  int main() {
  
      in_fp = fopen(SOURCE_CODE, "r");
      out_fp = fopen(CHARACTER_STREAM, "w");
  
      sourceToStream();
  
      fclose(in_fp);
      fclose(out_fp);
  
      return 0;
  }
  </code></pre>