variable can auto initialize itself?

This is a discussion on variable can auto initialize itself? within the C Programming forums, part of the General Programming Boards category; Below, the code is from the book "Programming Windows" written by Charles Petzold. It sure can be compiled and run ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    64

    variable can auto initialize itself?

    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?
    Last edited by thinhare; 09-12-2005 at 08:11 PM.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    If you declare a variable as static, it is automatically initialised to 0 the first time around. Then, when your function returns, and is called later, the value in the variable is the same as it was when it returned earlier. The value in the static variable will be retained until the process ends.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    Thank you!!! kermit. I just got it proved. I referred to K&R "the c programming language" but it doesn't say anything about this.

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    The static keyword is described in K&R section 4.6.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    yes, but it doesn't say it will be initialized to 0 by default if no explicit initialization.

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    True, you need to read a few lines more to section 4.9 for that. :P

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    you are damn right. ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21