Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an empty parameter list mean? [duplicate]

The book that i am currently reading states that when you declare a function in c that accepts no arguments, but you do not use the keyword void "the function call can pass any arguments it wants". so i attempted this.

int number();

int main(void)
{
    int x =1;
    printf("%d",number(x));
}

int number()
{
    return x;
}

but it did not compile??? how does this work?

like image 914
FutureSci Avatar asked Aug 10 '13 23:08

FutureSci


1 Answers

This is an obsolescent feature1 from before C was standardized, decades ago.

Never use it.

In ancient C, a decade before you were born, you could declare a function with no parameters. When you called it, the compiler would promote the arguments to default types and pass them to the function. Essentially, the compiler figured out the parameter declarations from the arguments in the call.

The function should still be defined with parameters, and they should match the way the function is called.


1 “Feature” is the wrong word. It was the way things were done at the time, since better ways were not yet widely developed. In a new programming language, this characteristic would be considered a deficiency.

like image 90
Eric Postpischil Avatar answered Oct 11 '22 18:10

Eric Postpischil