implicit declaration of functions with variable number of arguments.

D

dev

I hav read it in previous posts that we should declare function before
it is called.
but one question regarding implicit declaration ,that i found
accidently..

int main()
{
maw(32,3,95,38,20,15);
return 0;
}
void maw(int a,int b,...)
{
/*.....................*/
}
vikas@vikas-laptop:~$ gcc -W -O0 deu.c
deu.c:12: warning: conflicting types for ‘maw’
deu.c:7: warning: previous implicit declaration of ‘maw’ was here

I get warnings compiling this code....
but when i changed the return type of functions from void to int and
compile,ie,
int maw(int a,int b,...)
{
/*.....................*/
}
vikas@vikas-laptop:~$ gcc -W -O0 deu.c
deu.c:12: error: conflicting types for ‘maw’
deu.c:13: note: a parameter list with an ellipsis can’t match an empty
parameter name list declaration
deu.c:7: error: previous implicit declaration of ‘maw’ was here

I get errors.I cant understand Why there is only a warning in first
case and in second case it is leading to error.
please help me understand the fact behind it..
-
regards
 
G

Guest

I hav read it in previous posts that we should declare function before
it is called.

this is good practice.
but one question regarding implicit declaration ,that i found
accidently..

implicit declarations are twisty and should be avoided.
They make your code less reliable, harder to debug and less
efficient.
int main()

int main (void)

is better
{
        maw(32,3,95,38,20,15);

as there is no declaration is scope C assumes maw() has an unspecified
but fixed number of parameters and returns int.

        return 0;}

void maw(int a,int b,...)

but it doesn't return int
{
        /*.....................*/}

vikas@vikas-laptop:~$ gcc -W -O0 deu.c
deu.c:12: warning: conflicting types for ‘maw’
deu.c:7: warning: previous implicit declaration of ‘maw’ was here

so you get a diagnostic

I get warnings compiling this code....
but when i changed the return type of functions from void to int and
compile,ie,
int maw(int a,int b,...)

now it doesn't have a fixed number of parameters
{
        /*.....................*/}

vikas@vikas-laptop:~$ gcc -W -O0 deu.c
deu.c:12: error: conflicting types for ‘maw’
deu.c:13: note: a parameter list with an ellipsis can’t match an empty
parameter name list declaration
deu.c:7: error: previous implicit declaration of ‘maw’ was here

so you get a diagnostic

I get errors.I cant understand Why there is only a warning in first
case and in second case it is leading to error.
please help me understand the fact behind it..

to an extent its up to the compiler writer. He presumably thinks
he can't sensibly continue in the second case but not in the first.
It's a bit odd but since they are both wrong he can do what he wants.
 
J

James Kuyper

dev said:
I hav read it in previous posts that we should declare function before
it is called.
but one question regarding implicit declaration ,that i found
accidently..

int main()
{
maw(32,3,95,38,20,15);
return 0;
}
void maw(int a,int b,...)
{
/*.....................*/
}
vikas@vikas-laptop:~$ gcc -W -O0 deu.c
deu.c:12: warning: conflicting types for ‘maw’
deu.c:7: warning: previous implicit declaration of ‘maw’ was here

I get warnings compiling this code....
but when i changed the return type of functions from void to int and
compile,ie,
int maw(int a,int b,...)
{
/*.....................*/
}
vikas@vikas-laptop:~$ gcc -W -O0 deu.c
deu.c:12: error: conflicting types for ‘maw’
deu.c:13: note: a parameter list with an ellipsis can’t match an empty
parameter name list declaration
deu.c:7: error: previous implicit declaration of ‘maw’ was here

I get errors.I cant understand Why there is only a warning in first
case and in second case it is leading to error.
please help me understand the fact behind it..

Your code is incorrect in both cases. The reason is that in C90, if a
function is called without having been declared, an implicit declaration
is created for it. The implicit declaration specifies that the function
takes an unknown (but fixed) number of arguments, and returns an 'int'.
That implicit declaration remains in effect after the function call.

If you later provide a declaration for the function which returns a type
incompatible with int, or which takes a variable number of arguments,
that declaration is incompatible with the implicit declaration. Your
first declaration had both problems; your second declaration had only
the second problem.

You'll have to ask the developers of gcc why they choose a warning for
the first case, and an error for the second; the standard only requires
a diagnostic message - it doesn't distinguish between warnings and error
messages.

Implicit 'int' return types seemed convenient at the time the language
was first defined, and was retained for the sake of backwards
compatibility even after function prototypes were added to the language,
but it has been found to create maintenance nightmares. That is why it
was removed in C99. I strongly recommend not writing code that relies on
this "feature" of C90.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top