Thread: Deafult Arguments

  1. #1
    Registered User sarvsav's Avatar
    Join Date
    Oct 2013
    Posts
    25

    Deafult Arguments

    Hello Everyone,

    How to create default arguments in C? Is there any way to make default arguments ( i mean any alternative for them).

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,439
    The short answer is, you can't.
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The standard library occasionally uses 0 or NULL as "don't care"-type arguments, but there's no way to leave them off entirely.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Standard approach will be having 2 functions

    Code:
    void Initialize(int a, int b, int c)
    {
       InitializeEx(a,b,c,103);
    }
    
    void InitializeEx(int a, int b, int c, int d)
    {
      //Do you work
    }
    And call the first when you want to leave parameter d of InitializeEx with default value
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User sarvsav's Avatar
    Join Date
    Oct 2013
    Posts
    25
    Seems to very similar to function overloading, but i like the trick. Thanks for the help.

    Quote Originally Posted by vart View Post
    Standard approach will be having 2 functions

    Code:
    void Initialize(int a, int b, int c)
    {
       InitializeEx(a,b,c,103);
    }
    
    void InitializeEx(int a, int b, int c, int d)
    {
      //Do you work
    }
    And call the first when you want to leave parameter d of InitializeEx with default value

  6. #6
    Registered User sarvsav's Avatar
    Join Date
    Oct 2013
    Posts
    25
    By the way, i have used this: » Preprocessor magic: Default Arguments in C - Intelligent rumblings

    May be helpful for someone else.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arguments in MFC
    By guitarist809 in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2008, 07:23 PM
  2. Arguments
    By Suudsu2200 in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2007, 03:10 PM
  3. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  4. Arguments
    By Narf in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-09-2005, 08:11 AM
  5. Int Arguments
    By Scytz0 in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2003, 07:55 PM