consequences of calling a function with fewer arguments in C?

arguments, c, call, function

Solution

If you call a function with too few arguments and the compiler doesn't complain, then you're doing something wrong.

You can write a function declaration/definition that doesn't specify how many arguments it requires:

void func();
/* ... */
func();
func(arg1);
func(arg1, arg2);

All three of those calls will be accepted by the compiler, but at least two of them are incorrect.

That form of function declaration/definition has been obsolescent since the 1989 ANSI C standard.

Never use this form.

Functions declaration should always be written as prototypes, i.e., declarations that specify the number and type(s) of the parameters. As a special case, `(void)` denotes a function with no parameters.

void func(int arg);
/* ... */
func();           /* rejected by compiler */
func(arg1);       /* accepted -- but only if arg1 is of type int or convertible to int */
func(arg1, arg2); /* rejected by compiler */

If you manage to write code that calls a function with an incorrect number of arguments and get it past the compiler, the behavior is undefined. It might appear to "work", but it could blow up in your face when, for example, you compile it with a different compiler, or with the same compiler and different options.

One complication: some functions are variadic, taking a variable number of arguments. The most common example of this is `printf`. For variadic functions, the required arguments are typically specified by the function's documentation -- and it's just as important to get the arguments right. The difference is that, for variadic functions, your compiler won't necessarily tell you that a call is incorrect.

The `, ...` syntax (in the function declaration) and the macros defined in `<stdarg.h>` are the only legitimate way to write and use C functions that take a variable number and type(s) of arguments.

Problem

I wrote a function that that takes some argument and a pointer argument . when calling the function , sometimes I need to pass along the pointer for use inside the function and sometimes I don't . what are the consequences of calling a function with fewer arguments ? it compiles correctly and during runtime its still fine , but is this good programming ? is it better if I call the function with a dummy variable ? Thanks and sorry for beginner question .

Original source