![]() |
GCC:: Calling Function With Same Name But Different Return Type
After writing Why main() should not have void as return type? article the other day, suddenly one more question came up into my mind about the signature of main() function. In the earlier article I discussed about why 'void main()' should not be used when ANSI C expects the function main() to return an integer. But, how it is possible that main() is declared to return and integer while we can define it with return type as void. Sounds like a valid question, isn't it?
How I went about finding the answerIt was not a very easy task to start with as I needed to have a look at the code that calls the main() function. Well, since that was a bit difficult so I first thought of an easy way around. I used the following code : Code:
#include<stdio.h> Now, when I compiled the code, I got : Code:
~/practice $ gcc -Wall typemismatch.c -o typemismatch The question kept me quite for some time until I realized that through above example, I could not quite simulate what exactly happens with main() function. Then I re-structured the above example into : Code:
// test.c Code:
//test1.c Code:
//test.h Since, main was declared/called from a separate file and defined in a separate file so I had to re structure my example so that the function func() was declared/called from test.c but it is defined in test1.c (see code snippets above). Now, since I re-structured the code in the way I wanted, I went ahead with the compilation process : Code:
gcc -Wall test.c test1.c -o testCode:
~/practice $ ./test And to add on to the confusion : when I put the definition of func() in the same file from where it is being called(refer very first example) and remove the extern keyword from the declaration then I get compilation error. So if I see from a no error situation to an error situation only two things changed :
Then out of curiosity I did one more test, I changed test1.c to : Code:
#ifndef TEST_H_INCLUDED What the hell is happening? I had never seen something like this before in C or to be more precise I have never observed such behavior so closely. Anyways now situation became interesting. I consulted my colleagues in office about this behavior the very next day. Some answered that error should have been thrown by the compiler, some said that behavior could be undefined in this case while rest of them were also left confused over the issue. Then I searched on INTERNET a bit and found my answers in pieces, I tried to collect some important pieces of explanation and here it is :
Ya, this was my reaction after understanding all the points above. I assume the same must be the reason for my original question regarding conflict of return types in main() function. Now, one more thing came into my mind, If this is so, then If I define one more function with same name(func) in yet another separate file test2.c and compile it with test.c and test1.c then what will happen? Below is the complete code : Code:
// test.c Code:
//test1.c Code:
//test2.c Code:
//test.h Code:
~/practice $ gcc -Wall test.c test1.c test2.c -o test " I found two definitions and I don't know which one to link to?" This also cleared one more fact that is C does not support function overloading. The gcc version that I used for all the above tests is : Quote:
ConclusionTo Conclude, the above article is a bit of research that I did when I found a weird problem of C compiler/Linker not being able to identify the conflict in declaration and definition signatures when these are kept in different files. Stay tuned for more!!!!! |
Re: GCC:: Calling Function With Same Name But Different Return Type
Oh it's a good issue,I have learned a lot !~~Thank you for your sharing!
|
| All times are GMT +5.5. The time now is 14:17. |