Default Arguments in C

This is a discussion on Default Arguments in C within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void func(int a, int b=2) { printf("%d",a+b); } int main() { func(1,1); func(1); return 0; } This ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    24

    Default Arguments in C

    Code:
    #include <stdio.h>
    
    
    void func(int a, int b=2)
    {
        printf("%d",a+b);
    }
    
    
    int main()
    {
        func(1,1);
        func(1);
        return 0;
    }
    This code doesn't work in C. Is there a way to have variable b take a default value of 2 in C?
    Thanks!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,295
    No. Default arguments and function overloading are both specific to C++. There is no equivalent in C.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. default arguments
    By jackson6612 in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2011, 11:57 PM
  2. Default arguments
    By swgh in forum C Programming
    Replies: 5
    Last Post: 06-29-2007, 06:27 PM
  3. default Arguments
    By kishore in forum C++ Programming
    Replies: 2
    Last Post: 01-06-2007, 01:17 AM
  4. Default Arguments Question...
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2005, 05:27 PM
  5. Default Arguments
    By pianorain in forum C++ Programming
    Replies: 9
    Last Post: 12-20-2002, 07:40 AM

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