The difference between C89 and C99

This is a discussion on The difference between C89 and C99 within the C Programming forums, part of the General Programming Boards category; Note that was written in reply to your question that went "what compilers allow you to mix declarations and statements?" ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    Note that was written in reply to your question that went "what compilers allow you to mix declarations and statements?" That question isn't the same as what you're asking him now. Any expression of the right type can be an initializer, but you originally asked about variables being declared anywhere instead of just at the top. Are you sure you use this C99 feature in the code in question?

  2. #17
    Registered User
    Join Date
    Feb 2011
    Posts
    126
    Well, let's ask what style is best:

    1. Declare all variables at the top, then assign values after all declarations
    2. Declare all variables at the top, initialize all values whose initial values are known at the function's opening curly brace, then assign all other variables
    3. Declare all variables as soon as their initial values can be calculated
    4. Declare all variables right before they are used for the first time

    C89 allows the first two, C99 allows all four.

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    For C89 I would actually use number 5.

    5. Declare and initialize all variables to default values at the top of the function.

    Jim

  4. #19
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    All variables?

    So you mean you don't re-initialize the variable? How would you write this code:

    Code:
    int a=0, b=0, c=0;
    char szmystring [ 100]="";
    bool mybool;
    
    for (a=0;a<10;a++)
    {
      dosomething();
    }
    Also, I'd like to point out that some functions have 200 lines and a variable might not be used until the 150th line. Then it can be difficult to see where the declaration and usage match. That's one benefit of systems Hungarian notation, I suppose.

    Richard

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Richardcavell View Post
    So you mean you don't re-initialize the variable? How would you write this code:

    Code:
    int a=0, b=0, c=0;
    char szmystring [ 100]="";
    bool mybool;
    
    for (a=0;a<10;a++)
    {
      dosomething();
    }
    Also, I'd like to point out that some functions have 200 lines and a variable might not be used until the 150th line. Then it can be difficult to see where the declaration and usage match. That's one benefit of systems Hungarian notation, I suppose.

    Richard
    Assuming you will actually use all these variables someplace in your subroutine...

    Code:
    int a = 0;    // outer loop counter
    int b = 0;    // inner loop counter
    int c = 0;    // item count
    char szmystring [ 100]={0};  // title string
    bool mybool = false;    // exit flag
    
    for ( a = 0; a < 10; a++ )
      dosomething();
    Comments are your friends....

  6. #21
    Registered User
    Join Date
    Feb 2011
    Posts
    126
    but you're initializing the variable twice. That leads to the possibility that at some point you're going to change one initializer and forget to change the other.

    I'm tempted to say that if a variable starts being used 150 lines into the function, you should probably move that code to its own separate function. What do you think?

  7. #22
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,800
    Comments are your friends....
    Variable names that don't suck are your friends; comments are the nasty buggers who never return your lawnmower.

    but you're initializing the variable twice.
    A variable can't be initialized twice, but initialing it to a known value will help with debugging.

    Soma

  8. #23
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    So you mean you don't re-initialize the variable?
    You can only initialize a variable once. As long as the value is not a const you can assign a different value many times.

    If you always initialize your variables when you declare them to sensible values, not necessarily zero, you will not have problems because you are using an uninitialized variable.

    I prefer using meaningful names for variables, over Hungarian notation. I have never been a fan of Hungarian notation because it doesn't really tell you how the variable is actually going to be used.


    Jim

  9. #24
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by phantomotap View Post
    Variable names that don't suck are your friends; comments are the nasty buggers who never return your lawnmower.
    Yes you should use variable names like Loop1Ctr, and ExitFlag and they are indeed very helpful.

    But I would defy anyone to write extensive code without any comments and come back to it 5 years later and still be able to follow it... All non-obvious things should be commented for just that reason.

  10. #25
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Variables names and comments

    I hear so many conflicting things about good programming practice.

    Sure, you should label variables something descriptive, but what constitutes a good description? For example, in my code I iterate through command line parameters using:

    Code:
    int arg;
    
    for (arg=0;arg<argc; arg++)
     ...
    Now, what should I call it? i_argument, consoleargument, from1toargc, commandlineoptionscounter?

    And as to comments, I reckon the code should be easily readable. The best way to understand the code is to read it and figure out what it does. Good function names and variable names is a part of that.

    Richard

  11. #26
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,800
    *shrug*

    I agree that some commenting can be useful if done correctly; it wouldn't hurt to describe the expectation for function parameters for code built under the assumption that the caller takes responsibility for example.

    My comment (heh.) was specifically aimed at the kind of thing in you example. Clear, concise, and mundane code is a lot better than throwing a wall of text even if aimed at your future self.

    Soma

  12. #27
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    Now, what should I call it?
    If this value is never used again then a one letter variable is usually considered acceptable. Then later in your program you could reuse this variable in another loop.

    If the value is used later in your program then something like you have is better.

    Jim

  13. #28
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Richardcavell View Post
    I hear so many conflicting things about good programming practice.

    Sure, you should label variables something descriptive, but what constitutes a good description? For example, in my code I iterate through command line parameters using:

    Code:
    int arg;
    
    for (arg=0;arg<argc; arg++)
     ...
    Now, what should I call it? i_argument, consoleargument, from1toargc, commandlineoptionscounter?

    And as to comments, I reckon the code should be easily readable. The best way to understand the code is to read it and figure out what it does. Good function names and variable names is a part of that.

    Richard
    Ok... if you are working with things so common as argc and argv[] from a command line, everyone knows what they are, it's so well known that simple names are fully acceptable.

    For the non-obious stuff choose a *short* descriptive name. Keep in mind that some compilers only look at the first 16 or 32 characters of a name and you can sabotage yourself by being too verbose... for example : antidisestablishmentarianism and antidisestablishmentism could be seen as the same thing. Generally I name variables with a word for their function... Total, SubTotal, Loop1, etc... I don't try to write the encyclopedia britanica into a variable name...

    Comments to explain purpose, despite opinions to the contrary, have been VERY helpful to me over the years...
    For example... how far do you think you'd get in understanding this code without the comments...
    Code:
    // remote launch a file by association
    BOOL NetworkLaunchFile(SOCKADDR Client,PWCHAR FileName)
      { TCHAR     ext[MAX_TYPENAME]     = {0};    // file type name
        TCHAR     pgm[MAX_PROGRAMNAME]  = {0};    // expected pgrogram
        SLOT      pgs;                            // program slot number
        TYPEINFO  ti;                             // appoved type info
        // no launches in lockdown
        if (ServerLocked)
          { SendDatagram(Client,RM_STOP,NULL,0);
            return 0; }
        // get information about the file  
        PathGetTypeName(FileName,ext);
        if (! LoadTypeInfo(ext,&ti))
          { SendDatagram(Client,RM_STOP,NULL,0);
            LogPacket(Client,FileName);
            LogPacket(Client,L"File type not approved");
            return 0; }
        // guard against dogpiles
        if (RemLaunch)
          { SendDatagram(Client,RM_BUSY,&Setting.TimeOut,sizeof(BYTE));
            return 0; }
        // is the program already running
        _wsplitpath(ti.Program,NULL,NULL,pgm,NULL);
        pgs = TrackerGetProgramSlot(pgm);
        // program is not running
        if ((pgs == 0) && (ServerBusy > 0))  //busy by local program 
          { SendDatagram(Client,RM_BUSY,&Setting.TimeOut,sizeof(BYTE)); 
            return 0; }
        // program is running
        if (pgs > 0)
          { if ((wcsicmp(Programs[pgs]->Remote,ti.Remote) != 0) ||
                  !Programs[pgs]->AddFiles || Setting.NoAdd ) 
              { SendDatagram(Client,RM_BUSY,&Setting.TimeOut,sizeof(BYTE));
                return 0; } } 
        // launch the file
        LogPacket(Client,FileName);
        RemLaunch = 1;
        // prepare program name
        PathQuoteSpaces(ti.Program);
        // complete command line
        lstrcat(ti.Flags,L" ");
        lstrcat(ti.Flags,FileName);
        if (ShellExecute(NULL,L"Open",ti.Program,ti.Flags,NULL,SW_SHOW) < (HINSTANCE) 33)
          { SendDatagram(Client,RM_STOP,NULL,0);
            AddToLog(L"Error",L"Can't launch file");
            RemLaunch = 0;
            return 0; }
        // launch not expected
        if (PgmHasRemote(pgs))
          { SendDatagram(Client,RM_NOREMOTE,NULL,0);
            RemLaunch = 0;
            return 1; }
        // passed to client
        RemOwner = Client;
        SetTimer(MainWind,LaunchTimer,Setting.TimeOut * 1000,&TrackerNoLaunch);
        return 1; }
    Last edited by CommonTater; 03-23-2011 at 10:31 AM.

  14. #29
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,800
    Keep in mind that some compilers only look at the first 16 or 32 characters of a name and you can sabotage yourself by being too verbose
    Well, if you are on a compiler and linker that old, that is probably just for internal names; for external names, you'd probably just have the standard six.

    Soma

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Richardcavell
    Well, let's ask what style is best:

    1. Declare all variables at the top, then assign values after all declarations
    2. Declare all variables at the top, initialize all values whose initial values are known at the function's opening curly brace, then assign all other variables
    3. Declare all variables as soon as their initial values can be calculated
    4. Declare all variables right before they are used for the first time

    C89 allows the first two, C99 allows all four.
    I follow the rule of thumb that variables should be declared as locally as possible. For C89, that means declaring them at the top of the most local scope in which they are used.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C89 or C99
    By talin in forum C Programming
    Replies: 6
    Last Post: 05-26-2008, 12:45 PM
  2. C99 C89
    By salvadoravi in forum C Programming
    Replies: 4
    Last Post: 01-21-2008, 06:43 AM
  3. My C89 RANT!
    By evildave in forum C Programming
    Replies: 12
    Last Post: 12-07-2005, 09:15 PM
  4. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:54 AM
  5. C89 and C99
    By Brain Cell in forum C Programming
    Replies: 5
    Last Post: 02-23-2005, 11:21 PM

Tags for this Thread


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