title: Why does fread(fmtChunkId, sizeof(char), 4, fp); read 11 characters?
body: <p>Here is the part of the code that I'm having trouble with:</p>
  
  <pre><code>fread(fmtChunkId, sizeof(char), 4, fp);
  if(strcmp(fmtChunkId, "WAVE") != 0) {
      cout &lt;&lt; "Not WAVE format: " &lt;&lt; fmtChunkId &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>When I run the program, it prints <code>Not WAVE format: WAVE    e∞@</code> which is 11 characters long instead of 4. However, it works properly when I replace <code>fmtChunkId</code> with <code>type</code>, which confuses me because I declared those two variables the same.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string.h&gt;
  using namespace std;
  
  int main() {
      /*Get file path*/
      char filepath[261];
      cout &lt;&lt; "Please enter the file path of a .wav file: ";
      cin.getline(filepath, sizeof(filepath));
      FILE *fp = NULL;
      fp = fopen(filepath, "rb");
      if(!fp) {
          cout &lt;&lt; "Failed to open file: " &lt;&lt; filepath;
          return 0;
      }
      cout &lt;&lt; endl;
  
      /*Declarations*/
      char type[4];
      char riffChunkId[4];
      int riffChunkSize;
      char fmtChunkId[4];
      int fmtChunkSize;
      short audioFormat;
      short numChannels;
      int sampleRate;
      int byteRate;
      short blockAlign;
      short bitsPerSample;
      int dataSize;
  
      /*Read file data*/
      fread(riffChunkId, sizeof(char), 4, fp);
      if(strcmp(riffChunkId, "RIFF") != 0) {
          cout &lt;&lt; "Not RIFF format: " &lt;&lt; riffChunkId &lt;&lt; endl;
          return 0;
      }
      fread(&amp;riffChunkSize, sizeof(int), 1, fp);
      fread(fmtChunkId, sizeof(char), 4, fp);
      if(strcmp(fmtChunkId, "WAVE") != 0) {
          cout &lt;&lt; "Not WAVE format: " &lt;&lt; fmtChunkId &lt;&lt; endl;
          return 0;
      }
      fread(type, sizeof(char), 4, fp);
      if(strcmp(type, "fmt ") != 0) {
          cout &lt;&lt; "Not fmt: " &lt;&lt; type &lt;&lt; endl;
          return 0;
      }
      fread(&amp;fmtChunkSize, sizeof(int), 1, fp);
      fread(&amp;audioFormat, sizeof(short), 1, fp);
      fread(&amp;numChannels, sizeof(short), 1, fp);
      fread(&amp;sampleRate, sizeof(int), 1, fp);
      fread(&amp;byteRate, sizeof(int), 1, fp);
      fread(&amp;blockAlign, sizeof(short), 1, fp);
      fread(&amp;bitsPerSample, sizeof(short), 1, fp);
      fread(type, sizeof(char), 4, fp);
      if(strcmp(type, "data") != 0) {
          cout &lt;&lt; "Not data: " &lt;&lt; type &lt;&lt; endl;
          return 0;
      }
      fread(&amp;dataSize, sizeof(int), 1, fp);
  
      /*Print file data*/
      cout &lt;&lt; "RIFF Chunk Size: " &lt;&lt; riffChunkSize &lt;&lt; endl;
      cout &lt;&lt; "fmt Chunk Size: " &lt;&lt; fmtChunkSize &lt;&lt; endl;
      cout &lt;&lt; "Audio Format: " &lt;&lt; audioFormat &lt;&lt; endl;
      cout &lt;&lt; "Number of Channels: " &lt;&lt; numChannels &lt;&lt; endl;
      cout &lt;&lt; "Sample Rate: " &lt;&lt; sampleRate &lt;&lt; endl;
      cout &lt;&lt; "byteRate: " &lt;&lt; byteRate &lt;&lt; endl;
      cout &lt;&lt; "blockAlign: " &lt;&lt; blockAlign &lt;&lt; endl;
      cout &lt;&lt; "Bits Per Sample: " &lt;&lt; bitsPerSample &lt;&lt; endl &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>