newbie question on App Name

This is a discussion on newbie question on App Name within the Windows Programming forums, part of the Platform Specific Boards category; Hi, all I just picked up Charles Petzold's Programming Windows book and I'm slowly studying it --- so far it ...

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    newbie question on App Name

    Hi, all I just picked up Charles Petzold's Programming Windows book and I'm slowly studying it --- so far it seems to be a great book, but anyway...

    In the third chapter I have a simple (sample) program that declares, inside of WinMain the following line:

    Code:
    static TCHAR szAppName[] = TEXT("HelloWin");

    I understand pretty much everything about this line, that is TCHAR is a generic for of string of characters terminated with zero to be used in conversion between ASCII and UNICODE, if UNICODE identifier is defined ---- and obviously "HelloWin" is the name of the application, however my question is fairly simple ----

    Does the identifier STATIC, in front of the AppName, make the string AppName available to all functions, in other words even thought it should be "local" but it makes it "global" ???? or am I a bit off here????

    Thank you in advance for your help

    ............................
    matheo917

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    szAppName is a local variable,not global,so it is not accessible outside the function.
    Here the static keyword is used to declare a local variable with static storage duration.

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    what do you mean by "static storage duration"???

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well said Wledge. And not to insult matheo917's intelligence but I'm going to explain what you said differently (using an example).

    Example
    Code:
    int MyFunction(int param) {
      static int i = 0;
    
      if(!i)
        i = param;
    
      return i;
    }
    You would call MyFunction() with a number as its parameter.

    Code:
    #include <stdio.h>
    
    int main(void) {
      int i, j;
    
      i = MyFunction(6);
      j = MyFunction(10);
    
      printf("i = %d\nj = %d", i, j);
    
      return 0;
    }
    If you run the code you'll find that MyFunction() returns six in both calls. That is because the variable i inside of MyFunction() is static not automatic. If you changed the code so that i is not static you would find that the first call would produce a return of six and the second would return ten.

  5. #5
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    thanx for replies and don't worry it's not that easy to insult my intelligence....

    anyway, putting your example in my Microsoft Visual .NET and working a bit with the MSDN library and its explanation, I noticed there's few other, perhaps more or less ambiguous uses of the "static" identifier, most with which I was familiar...
    but in this case it wasn't as clear, mainly b/c i had to get acquainted with the so-called fact of "the function retaining its value.
    In essence that same variable, although declared inside the functin scope, it lives and dies with the program execution rather than the function itself, that's why (in your example) in the second call to MyFunction, the variable "i" had retained the value of (6) and since you had an invariant (if i == 0) then "i" didn't get assigned the value of (10)............

    anyway.....I just wanted to point out that sometimes it's easy to forget how it feels like or how it was when you didn't "know it all"..

    thanks again....

    -----------------
    matheo917

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Some people think using static variables is kind of sloppy though. Nonetheless, you can make functions that do something once then in each call after that do something completely different.

    Example
    Code:
    int MyFunction(void) {
      static BOOL init = FALSE;
    
      if(!init) {
        InitializeDrawingSurface();
      } else {
        DrawStuff();
      }
    }
    But code like that is why some consider static variables sloppy. Keep in mind that function works, but if it is indeed something that will be called as much as a drawing function would be it is prudent not to add unnecessary overhead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  4. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  5. newbie question about using windows app in Dev C++
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2002, 10:50 PM

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