C and defining a function prototype without parameters

This question is a branch of `f (void)`, which means the absence of parameters in C ++ 11 or C?

Several people answered this question and indicated that in C the value of a function prototype

void func () 

lies in the fact that func is a function that returns nothing (void) and whose parameters are currently unknown.

They further suggested that you could make this declaration, and then call the function with some arguments, such as:

 func (1, 2, 3); 

So, I did it, I did a test to make sure that it works, and I'm not surprised that he does it.

Here is the func.c that 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 func() function

 #include <stdio.h> void func (int a, int b, int c) { printf ( "%d, %d, %d\n", a, b, c ); } 

And here is my question (s)

Question 1:

When I run this program, I get, as expected, the output 1, 2, 3. This is a safe way to write code; that is, can we assume that ABI reliably guarantees that calling func() in main() will put 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 safe, then your answer changes if func() implemented in some language other than C?

+4
source share
2 answers

Are you asking about C or C ++?

C initially did not have prototype functions. You must write:

 extern void func(); 

and then define it:

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

C ++ prototypes of functions were added and made the above illegal. And also the declaration:

 extern void func(); 

a function that has no parameters is declared; calling it arguments was a mistake, because they determined it with arguments.

C then prototypes of functions from C ++ are added. But in order to avoid breaking existing code, he did not require them and processed

 extern void func(); 

still: function taking an unknown number and types of parameters. Therefore, he also added:

 extern void func(void); 

as a special way to say that a function does not accept any parameters. C ++ then added this special case for reasons C.

A general rule in C ++ is to simply write:

 extern void func(); 

The only time you use a form with void is in the header, which should be compatible with both languages. In C, of โ€‹โ€‹course, this form does not do what you want, so you need to add void . (At the moment. From what I understand, C is outdated form, and therefore in the future may behave exactly like C ++ in respect of this.)

EDIT:

By watching this. From the C11 standard, ยง6.11.6:

Using function declarators with empty parentheses (not a prototype type of declarator formats) is an obsolete feature.

Do not do this in C.

+10
source

Q1. If the arguments are of the correct type, then MAYBE it will work (maybe it will). But try func(1.2, "blah"); and look what he does - he can "work" in the sense that he is not crashing - he will probably compile.

Q2. See Answer to Q1. This is not at all safe.

+1
source

Source: https://habr.com/ru/post/1498360/


All Articles