View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12933 Members
Join this group to post and comment.

Mismatch in arguments passed to a function and what a function can take

Why the below code works fine without any compile time or runtime errors?

#include “stdio.h”

int main() {
    a('c');
    return 0;
}

a(char* ch, int i) {
    printf("%c\n", ch);
}
I know the answer but thought to post it as a query before sharing the cause of this behavior.

-Pradeep
vivek.m
vivek.m • Apr 25, 2009
It wont work in C++ as in C++, it's necessary to declare function before using. Moreover, function arguments while calling that function must match with the function declaration.

On old C compilers, your program may work as C allows dummy arguments. I think this behavior is borrowed from FORTRAN and gives the developer flexibility to add new arguments to functions at later date without actually modify the existing code. The C code wont shout about the arguments until you provide the function with that name.

If the caller passes the argument, it is used. If the caller does not pass the argument, it holds either garbage or null value (not sure about this).

I see this flexibility as very limiting as you can provide dummy arguments to very limited types - int/float/char only. If you put any pointer as dummy argument and then using it inisde code will lead to crash.

Anyway, if you have better/valid reasons for why this is allowed or how can it be helpful in programming, please share.

Thanks for asking this question!
ms_cs
ms_cs • Apr 25, 2009
vivek.m
It wont work in C++ as in C++, it's necessary to declare function before using.
if you declared as a(char*,int);,then it wont work...compile time error will arise...I think the place where declaration resides plays an important role
bharathkumarp
bharathkumarp • Apr 25, 2009
i think once u enter the return 0 in the main function it will never come to the called function. so it come out without showing any error..............
silverscorpion
silverscorpion • Apr 27, 2009
Pardon me. But when I tried to execute this program, I got some errors in my 16 bit turbo C compiler. Is it happening only to me???
rama_krish627
rama_krish627 • Apr 27, 2009
I also treid to execute this code in c++, but it is showing compile time error.

waiting for the better answer from anybody.........
slashfear
slashfear • Apr 28, 2009
Hi guys,

Silver and Ram you will surely get a compile time error stating " In function ‘int main()’: cpp:5: error: ‘a’ was not declared in this scope " as well as "cpp:11: error: ISO C++ forbids declaration of ‘a’ with no type" when you compile the above code.

Here the first error state's that "'a' was not declared in this scope" so it means that you are trying to use a variable you didn't define.
and second error state's that "ISO C++ forbids declaration of ‘a’ with no type" so it means that your declaring a function with no data type.

So I am really wondering how the code will compile and run with out any errors for Pradeep...... could you give us details of your compiler and could you be specific about your question......!!!
bharathkumarp
i think once u enter the return 0 in the main function it will never come to the called function. so it come out without showing any error.
No this is not the correct reason. The return code at the end of main function is just like returning some value from any other function. The return code here specifies the execution state of the code (i.e., "return 0;" means that the code executed successfully). The code to call the function 'a' comes before "return 0;" so it will get executed.

Read below post for rational behinf the working of the 'C' code even after parameter type mismatch.


-Pradeep
ms_cs
if you declared as a(char*,int);,then it wont work...compile time error will arise...I think the place where declaration resides plays an important role
silverscorpion
But when I tried to execute this program, I got some errors in my 16 bit turbo C compiler. Is it happening only to me???
rama_krish627
I also treid to execute this code in c++, but it is showing compile time error.
slashfear
Hi guys,
So I am really wondering how the code will compile and run with out any errors for Pradeep...... could you give us details of your compiler and could you be specific about your question......!!!
Sorry for the delay in response and not clearly specifying that the code i am talking about is a 'C' code. The code gives error in C++ because C++ has strong type checking.


-Pradeep
vivek.m
It wont work in C++ as in C++, it's necessary to declare function before using. Moreover, function arguments while calling that function must match with the function declaration.
Yes this is correct, the code does not work in C++. C++ has strong type chacking and it's necessary that the function is defined (or atleast declared) before it's use.


vivek.m
On old C compilers, your program may work as C allows dummy arguments. I think this behavior is borrowed from FORTRAN and gives the developer flexibility to add new arguments to functions at later date without actually modify the existing code. The C code wont shout about the arguments until you provide the function with that name.
Yes, this is also correct. Just to add more details:
1. The code is 'C' code and hence it is not necessary to give function prototype. This justifies the working of code in absence of prototype.

2. According to K&R,
"If there is no function prototype, a function is implicitly declared by its first appearance in an expression. If a name that has not been previously declared occurs in an expression and is followed by a left parentheses, it is declared by context to be a function name, the function is assumed to return an int, and nothing is assumed about its arguments, all parameter checking is turned off. This special meaning of the empty argument list is intended to permit older C programs to compile with new compilers. In the older programs the parameters are named between the parentheses, and their types are declared before opening the left brace; undeclared parameters are taken as int."
This justifies the working of code even after parameter mismatch.

3. When we do not provide sufficient parameters it takes the values from the stack (the value may be garbage for that particular function). This is similar to something that happens with printf() when the number of arguments and format specifiers varies, e.g., printf("%d %d", a); works fine and it takes the second value from the function stack.


vivek.m
If the caller passes the argument, it is used. If the caller does not pass the argument, it holds either garbage or null value (not sure about this).
As i mentioned in point 3 above, it will hold the garbage value.


vivek.m
I see this flexibility as very limiting as you can provide dummy arguments to very limited types - int/float/char only. If you put any pointer as dummy argument and then using it inisde code will lead to crash.
Not necessary, as in the example i have given, i have passed a char to char pointer and it works. Similarly, i can pass a int pointer to int and it will work and use it suitably. The variable in the function definition should be big enough to hold the value passed during the function call and it will work.


-Pradeep

Share this content on your social channels -