Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Pure C - function with optional arguments only

Posted on 2003-11-03
Last Modified: 2010-04-15

I want to declare a function, which has no required parameters, but may have optional, and do it in pure C.

It's simple for functions having at least one parameter:
   void my_func( char *sFirst, ... );
and then accessing the optional using arg_list.

I want to define something like
   void my_func( ... );
but it's not valid :(

I'll appreciate your help.
Question by:migoEX
LVL 45

Expert Comment

ID: 9669764
man 3 getopt

something like
while ((ch = getopt(argc, argv, "a:dhf:l:m:np:rs:v")) != EOF)
should serve your purpose

the above line of code is from syslogd source code

Assisted Solution

Ajar earned 30 total points
ID: 9669871
hey you can always declare a pointer to a function without no args e.g

void ( * function)();

void  a_function_implementation(int  param1);
   // The param may not have been passed so the question is how
  // do you intend to find out wether a param has been passed or not;

int main(int argc,char* argv[])
    function = a_function_implementation;

Author Comment

ID: 9670048
There is 1 problem left:

I want this function to be exported, and used by "external" functions. If in my H file I declare the function without parameters, the caller won't be able to compile a code, which is passing parameter. And vise versa - if I define it with parameter, a call without passing parameters won't compile.

PS: I'm not sure it's possible :)  but may be I'm missing some solution
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

LVL 45

Expert Comment

ID: 9670120
Even I would be interested in the solution if this was possible ...

Accepted Solution

mtmike earned 170 total points
ID: 9670425
You can declare the function with an empty argument list, eg

void myfunc();

In C (not C++) this functions takes any number of arguments. The above means "arguments are still unspecified". You can specify the arguments later on when defining the function.

The above is also the reason that functions without arguments should be defined as

void myfunc(void);

Author Comment

ID: 9670508
Thanks, mtmike!

That's exactly what I was looking for :)

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question