Thread: Question on C syntax

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    Question on C syntax

    hi,
    I came across this code online on a tutorial site. This part of the code confused me.

    Code:
    /*
     * parse--split the command in buf into
     *         individual arguments.
     */
    parse(buf, args)
    char *buf;
    char **args;
    {
        while (*buf != NULL) {
            /*
             * Strip whitespace.  Use nulls, so
             * that the previous argument is terminated
             * automatically.
             */
            while ((*buf == ' ') || (*buf == '\t'))
                *buf++ = NULL;
    
            /*
             * Save the argument.
             */
            *args++ = buf;
    
            /*
             * Skip over the argument.
             */
            while ((*buf != NULL) && (*buf != ' ') && (*buf != '\t'))
                buf++;
        }
    
        *args = NULL;
    }
    What confused me was the local variables declared ouside the opening brace of the function definition. I have also seen such code before once but thought it was a typo. But obviously this is a valid syntax. Can someone clear this for me please.

    -Livin

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes, that is K&R syntax for declaring a function (pre-dates ANSI-C).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    9
    Salem is correct. Depending on the material you're reading, you may see them more often than you might have expected. Anyway, it's really just the old way to define a function:
    Code:
    parse(buf, args)
    char *buf;
    char **args;
    {
        /* code here */
    }
    is the same as
    Code:
    int parse (char *buf, char **args)
    {
        /* code here */
    }
    If you don't see a return type, it is automatically int. That's really the only nuance of the syntax that could make it difficult to understand in my opinion.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    This may be obvious, but you do not want to use the K&R style functions in new code. K&R declarations are not prototypes, so the compiler cannot check to make sure you're calling a function properly. If you try to provide a prototype for a K&R style function, problems will occur with certain types (_Bool, char, short, and float) due to promotion issues.

    In short, knowing K&R style is good for reading old code, but it should never be used in new code.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Thank you all. I was almost convinced I had some basic concepts missing. Now it makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax question: &(++x)
    By yoshiznit123 in forum C Programming
    Replies: 8
    Last Post: 06-02-2006, 10:40 PM
  2. Syntax Question
    By Beaner in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2006, 02:05 PM
  3. syntax question
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2004, 07:58 AM
  4. question on syntax
    By blue_gene in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2004, 09:50 PM
  5. syntax question
    By Neoground1 in forum C++ Programming
    Replies: 1
    Last Post: 10-20-2002, 10:24 PM