Simple C question

Discussion in 'Mac Programming' started by neutrino23, Sep 13, 2010.

  1. macrumors 65816

    Joined:
    Feb 14, 2003
    Location:
    SF Bay area
    #1
    I'm working my way through Learn C on the Mac.

    When he discusses variable names he says they should start with either and upper or lower case letter. But when he talks about globals he says you declare a global like this.


    int gmyVar;

    Does this imply that any name starting with a g is a global? So the following

    int goodOne;

    declares a global even though I didn't intend that?


    Thanks
     
  2. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    No.

    It is just a stupid practice by some programmers to indicate that a particular variable is global in scope. Some also indicate the type of a variable by prefixing the variable name with i for instance (for int) or ui (unsigned int). Personally I dislike that approach.

    C does not use the name of a variable to indicate its scope nor its type at all. That is purely based on the scope of the variable (whether file scope, external scope or local scope) and the type that it was declared to have.
     
  3. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #3
    Nvm, just read crom's post.
     
  4. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #4
  5. macrumors 6502

    Joined:
    Apr 24, 2008
    #5
    That sounds like a value judgment. :)
     
  6. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #6
    Yup, as others have said, you can call a variable whatever you want. The recommended practice is a suggestion only, and it's designed to help you remember what the variables are while you're coding. It's meant to help make the code more readable, but it's entirely up to you whether to follow a (any) convention or not.

    If you end up working as a software developer for a company, you might find that they have existing coding conventions that you are expected to follow. For example, at a company I worked for, class variables always started with c (cString), passed parameters started with p (pString), and globals started with g (gString). Constants were always uppercase to distinguish them from other variables (STRING_LENGTH). And so on.

    Find a convention you like -- any one -- and stick with it. Consistency is the key to understandable code.
     
  7. Guest

    Joined:
    Mar 6, 2007
    #7
    From the web-page in question:

    Code:
    1   #include "sy.h"
    2   extern int *rgwDic;
    3   extern int bsyMac;
    4   struct SY *PsySz(char sz[])
    6      {
    7      char *pch;
    8      int cch;
    9      struct SY *psy, *PsyCreate();
    10      int *pbsy;
    11      int cwSz;
    12      unsigned wHash=0;
    13      pch=sz;
    14      while (*pch!=0
    15         wHash=(wHash<>11+*pch++;
    16      cch=pch-sz;
    17      pbsy=&rgbsyHash[(wHash&077777)%cwHash];
    18      for (; *pbsy!=0; pbsy = &psy->bsyNext)
    19         {
    20         char *szSy;
    21         szSy= (psy=(struct SY*)&rgwDic[*pbsy])->sz;
    22         pch=sz;
    23         while (*pch==*szSy++)
    24            {
    25            if (*pch++==0)
    26               return (psy);
    27            }
    28         }
    29      cwSz=0;
    30      if (cch>=2)
    31         cwSz=(cch-2/sizeof(int)+1;
    32      *pbsy=(int *)(psy=PsyCreate(cwSY+cwSz))-rgwDic;
    33      Zero((int *)psy,cwSY);
    34      bltbyte(sz, psy->sz, cch+1);
    35      return(psy);
    36      }
    This makes my eyes bleed.
     
  8. macrumors 68030

    PinkyMacGodess

    Joined:
    Mar 7, 2007
    Location:
    Midwest America.
    #8
    It's a 'convention', an element of style. If you program yourself, you can use what you like but if you program for or with others they very often have directives that you have to follow.

    Well, and if a program gets very complicated and long, anything can help get to the end point. Inline comments are something that you should get used to doing also. If not for others, then for yourself...

    Documentation is like sex. A little is better than none...
     
  9. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    And? The vast majority of things on programming sites are going to be based on opinion rather than objective or rational thought processes.
     
  10. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #10
    If I wanted goodOne to be a global, I would name the variable gGoodOne.
     
  11. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #11
    BLEH! Me too.

    I'd lose the type prefixes on the variables. If I declare x as an int, then it shouldn't be too hard to remember that or look up a few lines in the code -- no need to call it intX or iX. Even better, call it a more descriptive name than x, such as "month" or "numOfWidgets".

    I do, however, like the convention of using c and g to prefix variables that are of class member or global scope.
     
  12. macrumors 6502a

    Joined:
    Jun 27, 2010
    #12
    You can name a variable anything you want. The name will not determine if it is a global or a local variable.

    However, where you define it in the code and what keywords you use along with it will determine what kind of variable it is.
     
  13. macrumors 6502a

    milbournosphere

    Joined:
    Mar 3, 2009
    Location:
    San Diego, CA
    #13
    I hope to god that code was computer generated by some form of design program...otherwise that programmer needs to be fired.
     
  14. macrumors 6502a

    iggypod

    Joined:
    Jun 23, 2010
    #14
  15. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #15
    A, Its called a contrived example, and most if not all tutorials, books and examples suffer from it.

    B, its visual basic. Its near impossible to get nice looking code with VB.
     
  16. macrumors 6502a

    Joined:
    Apr 1, 2007
    #16
    That's not VB. VB can be made to look nice.
     
  17. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #17
    Oops, read Studio as Basic.
     
  18. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #18
    prefixing variable names is archaic. with the widespread use of IDEs like Eclipse, you have tools that let you easily find out the type and scope (and comments) of anything.

    best practice, imho, remains using descriptive names, useful comments, and whitespace for readability.
     
  19. thread starter macrumors 65816

    Joined:
    Feb 14, 2003
    Location:
    SF Bay area
    #19
    Thanks. Now I get it. In the first reading I thought that the "g" prefix had the effect of making the variable global. Now I see it is more subtle. If you declare the variable like this:

    Code:
    int	MyVar;
    int main (int argc, const char * argv[]) { 
    MyVar = 10;
    then it is global.

    If you declare it like this

    Code:
    int main (int argc, const char * argv[]) { 
    int	MyVar;
    MyVar = 10;
    Then it is not global. Kind of subtle.

    I've worked a lot with some machine language, DEC Macro-11, Fortran-11 and Forth and Applescript. Now I'm going to make the run through C, Objective C and Cocoa.
     
  20. macrumors 6502

    Joined:
    Apr 24, 2008
    #20
    Just remember that a variable is available from the point it's declared to the end of the current scope. If it's declared outside of any function body, the "current scope" extends to the end of the compilation unit (over-simplified: to the end of the file).
     
  21. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #21
    It's nice to just look at source code and know what it is doing without having to use any tools. Especially nice if you know something is wrong in a few thousand lines of code and you don't know where. I'd say using clear variable names that don't require me to use any tools speed up reading source code by a factor ten at least.


    Scope is not the only thing, lifetime is another. extern and static variables exist from the moment the program starts to the moment it finishes. Local variables in a function exist only while the function is running; and they can exist multiple times if a function is recursive (but you wouldn't usually care about that) or is used in multiple threads. So returning the address of a local variable from a function is asking for trouble - because the variable stops existing when the function returns. extern and static variables exist only once which is important for multithreaded code - if you have a global variable "int i" and two threads execute a loop "for (i = 0; i < 1000; ++i) ..." then surprising things will happen.
     
  22. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #22
    To expand, if you declare a variable in a .c or .m file, the variable will only be available to the code that is in that file. It will not be strictly global unless it is declared in a .h file, and then only global within other .c or .m files that import that header (directly or indirectly).
     
  23. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #23
    Some of us still prefer hand tools, home grown veggies, and using vi in a monochrome terminal window to code in C and ARM assembly language (on our MacBook Pros for our iPhone 4's).
     
  24. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #24
    fair enough. i still use vi, and pre-IDE, i still hated hungarian notation.
     
  25. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #25
    Danger ahead! A variable declared as global in a file _is_ global, whether it is in a header file or not, and whether it is intended or not. If you (or someone else) tries to declare another variable with the same name in another file, then you don't get two variables, you have _one_ variable. Great source of bugs.

    If you want a variable with application life time, and you don't want it accessed from another file by accident, make it static.
     

Share This Page