Below, the code is from the book "Programming Windows" written by Charles Petzold. It sure can be compiled and run correctly. Something weird happened to the variable "fAngle".
Code:
void FillBuffer (PBYTE pBuffer, int iFreq)
{
static double fAngle ;
int i ;
/* below 3 lines are added by myself for testing purpose */
char buff[1000];
sprintf(buff, "%ld", fAngle);
MessageBox(NULL, buff, 0, MB_OK);
for (i = 0 ; i < OUT_BUFFER_SIZE ; i++) {
pBuffer [i] = (BYTE) (127 + 127 * sin (fAngle)) ;
fAngle += 2 * PI * iFreq / SAMPLE_RATE ;
if (fAngle > 2 * PI)
fAngle -= 2 * PI ;
}
}
Please look at the code and you will find no intialization taken before using it. So I tried to display the value of it using MessageBox. No error reported while compiling and running. The value shown at first time was 0, then changed, then changed...
I had thought that was because MSVC done that for me. Thus I tried to display the variable "i" in the same way. Error of uninitialized variable being used reported by MSVC compiler.
I suspected maybe the keyword static or double was the answer and had googled a lot but nothing supporting my guess.
Anyone knows the answer?