Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it not possible to use implicitly typed function arguments with explicitly typed function arguments?

Tags:

c

syntax-error

If i were to compile the below code:

// x is implicitly typed as int
void foo(x, char y) {
    return;
}

A syntax error would occur (expected identifier), but i am not sure why this syntax is not valid.

However:

void foo(int x, char y) {
    return;
}

compiles successfully.

So why is this syntax not valid? I am no expert at C so i'm not sure if this is a silly question or not.

like image 352
iocode Avatar asked Nov 25 '25 03:11

iocode


1 Answers

Implicit argument types, or more accurately a function definition with an identifier list, was part of the original K&R design of C. In those days, you could either have a definition like this:

int foo(x, y) 
{
    return 0;
}

In which case all parameters default to type int, or a set of declarations before the body:

int foo(x, y) 
   int x;
   char *y;
{
    return 0;
}

To specify the types of the arguments. In neither case were the types of the parameters known to the caller.

Specifying the types directly in the definition was added to the language later, which also added the ability to specify the types of a function's arguments in a declaration, allowing callers to know the types of the parameters.

So there's wasn't much reason to have a hybrid of the two, as one was made to improve on the other.

like image 134
dbush Avatar answered Nov 27 '25 16:11

dbush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!