Thread: Really weird function declaration??

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    Really weird function declaration??

    Hi,
    While porting some really old C code, I found a lot of function prototypes that look like this:
    Code:
    #ifdef _NW_STDCPROTO_
    int
    nl_init(
    	NL_INFO *info
    )
    #else
    int
    nl_init(info)
    	NL_INFO *info;
    #endif
    {
    ...
    }
    The strange one is in the #else clause. Why does the init parameter only have the variable name in the parenthesis, and then AFTER it declares it along with the variable type?
    Is this the way C worked back in the stone age?
    How should you write a function prototype in that case?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that's the pre-ANSI model of declaration. Most compilers use the former nowadays.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think the else one is a K&R style function declaration. I suppose you don't need the ifdef at all and just use the first.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > Is this the way C worked back in the stone age?
    Yes, this is colloquially called K&R C

    > How should you write a function prototype in that case?
    K&R C didn't have prototypes, only function declarations.
    It would typically be written as
    extern int nl_init();

    Keeping definition and use in agreement without prototypes was one of the reasons for the historic lint program. Prototypes largely fixed the problem (though variadic parameters are still subject to the old rules), and commercial lint programs now find lots of other things to complain about.
    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.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Thanks.
    I guess I'll just put the standard prototypes in the header file and save myself a lot of headaches.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Oh, another question about the K&R style functions:
    Some of the functions are defined like this:
    Code:
    int
    do_call(cid, inbuffer, inlength, outbuffer, outlength, count, ret)
    NL_CONNECTION_ID cid;
    unsigned short *inbuffer, *outbuffer;
    unsigned inlength, outlength;
    unsigned count;
    int ret;
    { ... }
    If you notice, the order of the names in the parameter list is different than the order that they are declared before the function body.
    In this case, which order will the compiler use? i.e. Would you call the function the way it looks, or do you pass the parameters in the order they're declared: do_call( cid, inbuffer, outbuffer, inlength, outlength, count);

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The parameters are used in the order they are listed in the parenthesis in the function header, not in the order you define what type which parameter is.

    This is so that you can do (stupid) things like this:
    Code:
    int blah(a, b, c, d, e, f)
    char *a, *c, *e;
    int b;
    float d, f;
    {
    }
    That saves wear and tear on the fingers. (Remember, text-editors in the day when C was invented didn't have click-drag-right-click-select-copy-move-select-right-click-paste stuff. And nor did they have much of the auto-completion that we have nowadays.

    [There's a joke in programmer circles about "cobol fingers", since Cobol is a very "wordy" language, you'd wear them out, so "cobol fingers" is when your fingers are just short stumps rather than "full length"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM