Thread: Default values in function prototypes

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    Question Default values in function prototypes

    Hi,

    I would like to know if "C" allows one to put default values in function prototypes like in "C++?"

    Here is an example:

    int SetName( char *pchName, BOOL fNewName = TRUE );

    Thanks!

  2. #2
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86

    Yep :)

    It does

    Greetings
    cody
    #include "reallife.h"

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    I tried it with Visual C++ 6, and it doesn't allow it. Is it actually in the ISO C standard?

  4. #4
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86
    Yes, it should be ANSI/ISO standard. But standards don't bother MS compilers very much

    What code did you try to compile?!

    Greetings
    cody
    #include "reallife.h"

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    This is what I have in my header file:

    /* sc.h */

    sc_error_t DllDeclare SCSetUser_Privilege_IPSecAPIKey(
    char *pchUserKey,
    char *pchPrivilegeKey,
    BOOL fSA = TRUE,
    BOOL fNewPW = TRUE );

    This is in my source file:

    /* sc.c */

    sc_error_t DllDeclare SCSetUser_Privilege_IPSecAPIKey(
    char *pchUserKey,
    char *pchPrivilegeKey,
    BOOL fSA,
    BOOL fNewPW )
    {
    sc_error_t eErr = SC_OK;

    ...

    return eErr;
    }

    On the prototype line of BOOL fSA the following errors:
    syntax error : missing ')' before '='
    and
    'SCSetUser_Privilege_IPSecAPIKey' : initialization of a function

    I have also touched base with the comp.lang.c newsgroup. So far I received one reply on the question. ("No")

    I have now decided to write Dennis Ritchie himself to find out.

  6. #6
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86
    Have you tried to compile it without the default values?
    #include "reallife.h"

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    Then it is fine!

  8. #8
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86

    Hmm

    Well, i guess my "jep it does" was wrong then and default arguments are indeed a c++ feature. I changed from c to c++ a longer time ago, but really thought I could remember using default arguments in normal c. Hmm....damn...I'm getting old

    Sorry
    Greetings
    cody
    #include "reallife.h"

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    Yea, I've been on C++ since '94 and didn't know if C was updated to handle default values for function prototype parameters.

    We're busy with a very intense project that is cramped into a very short time. Unfortunately we have to make use of legacy C code.

  10. #10
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86

    Well...

    ...good luck then

    ...and I could have sweared, that I've used default arguments in a C proggy...well...

    Have a nice day
    cody
    #include "reallife.h"

  11. #11
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Re: Well...

    No, not in C89 one can't , I dunno about C99
    Originally posted by cody
    ...good luck then

    ...and I could have sweared, that I've used default arguments in a C proggy...well...

    Have a nice day
    cody

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    Thumbs down

    I was in contact with Dennis Ritchie of "Kernigan and Ritchie" fame, and he said he doesn't think it is in C99, and definitely wasn't in previous standards.

    It is such a pity!

  13. #13
    Unregistered
    Guest
    I know for a fact that C does not support default arguments. Perhaps in the newest draft they've added it, but in "classic C" (meaning as it was originally made) it does not support default arguments.

    The reason it may be working for you is because you're using a C++ compiler, and compiling as C++ rather than C, which, C++ handles C and then C++, thus ending up with it accepting the arguments. That's my guess.

    Quzah.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    I originally started this thread because it was not working under Visual C++, and thought that perhaps it was just MS that did not comply with the C standard, but alas, the standard says no!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Replies: 12
    Last Post: 10-16-2008, 02:49 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM