Writing computer generated music to a .wav file in C
Endianness
Your program works on little endian machines only.  If you care for it to work on any machine, you would need to fix up all of the fwrite() calls to output specifically in little endian format.
Use struct for header
I would suggest creating a struct for the file header.  That way, you could fill in all the fields (in an endian correct manner) and then do one single fwrite().  From the code it looks like you can combine all the fields into a single header, but if it makes more sense you could have multiple headers (RIFF header, format header, data header, etc).