Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C and defining a function prototype with no parameters

Tags:

c++

c

This question is an offshoot of `f(void)` meaning no parameters in C++11 or C?

Several have answered that question and opined that in C, the meaning of the function prototype

void func ()

is that func is a function returning nothing (void) and whose parameters are unknown at this time.

Further they have opined that one could make this declaration and then invoke the function with some arguments such as:

func (1, 2, 3);

So, I did this, I made a test to verify that this works and I'm not surprised that it does.

Here is func.c, which contains main()

#include <stdio.h>

extern void func ();

int main (int ac, char ** av)
{
  func (1, 2, 3);

  return 0;
}

And here is func1.c which contains the function func()

#include <stdio.h>

void func (int a, int b, int c)
{
  printf ( "%d, %d, %d\n", a, b, c );
}

And here are my question(s)

Question 1:

When I run this program, I get, as expected the output 1, 2, 3. Is this a safe way to write code; i.e. can one assume that the ABI will reliably ensure that the invocation of func() in main() will put the three parameters in the right places (registers, stack, whatever) for func() to find them?

Question 2:

If the answer to 1 above is that it is a safe thing to do, then does your answer change if func() is implemented in some language other than C?

like image 505
amrith Avatar asked Aug 22 '13 11:08

amrith


People also ask

How do you define a function with no parameters?

Defining a Function Without Parameters We can call the function by typing its name followed by parentheses () . When we call this function, it will print the current date.

Can a function have no parameters in C?

If a function takes no parameters, the parameters may be left empty. The compiler will not perform any type checking on function calls in this case. A better approach is to include the keyword "void" within the parentheses, to explicitly state that the function takes no parameters.

Can we create function without parameter?

How to Create a Function Without a Parameter in SQL. Line one creates a function then named the function “YTDSALES()”. You can give your function any name. Remember to add parenthesis to the name of the function when without a parameter.

Do function prototypes need parameter names?

In a prototype, parameter names are optional (and in C/C++ have function prototype scope, meaning their scope ends at the end of the prototype), however, the type is necessary along with all modifiers (e.g. if it is a pointer or a reference to const parameter) except const alone.


2 Answers

Are you asking about C or about C++?

C did not originally have function prototypes. You'd write:

extern void func();

and then define it:

void func( a, b, c )
    int a;
    int b;
    int c;
{
    //  ...
}

C++ added function prototypes, and made the above illegal. And the declaration:

extern void func();

declared a function which had no parameters; calling it with arguments was an error, as was defining it with arguments.

C then added function prototypes from C++. But to avoid breaking existing code, it didn't require them, and treated

extern void func();

as before: a function taking an unknown number and types of parameters. So it also added:

extern void func(void);

as a special way of saying that the function doesn't take any parameters. C++ then added this special case for reasons of C compatibility.

The general rule, in C++, is to just write:

extern void func();

The only time you'd use the form with void is in a header that had to be compatible with both languages. In C, of course, this form doesn't do what you want, so you have to add the void. (For now. From what I understand, C has deprecated the older forms, and so could, in the future, behave exactly like C++ in this respect.)

EDIT:

Having looked it up. From the C11 standard, §6.11.6:

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

Don't do it in C.

like image 51
James Kanze Avatar answered Nov 07 '22 15:11

James Kanze


Q1. If the arguments are the right type, then MAYBE it will work (it probably will). But try func(1.2, "blah"); and see what that does - it may "work" in the sense that it doesn't crash - it will certainly compile.

Q2. See answer to Q1. It is not at all safe.

like image 1
Mats Petersson Avatar answered Nov 07 '22 14:11

Mats Petersson