By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,938 Members | 2,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,938 IT Pros & Developers. It's quick & easy.

calling functions in C

P: n/a
Hi, Can someone please explain me what's going on during a call to a
function. Specifically, I wanted to know what's going on in Stacks.

Why is it ok to pass only 2 parameters or 5 parameters when the function
prototype requires 3.

thanks.

Nov 14 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Yuan Zhong wrote on 26/07/04 :
Can someone please explain me what's going on during a call to a
function.
The execution is detoured to a subroutine and resumed once the end of
the function or 'return' is reached. The values of the parameters are
redable from the fuction. Typed pointer parameters can be dereferenced.
A value can be returned.
Specifically, I wanted to know what's going on in Stacks.
It depends on the implementation. Some have no stack.
Why is it ok to pass only 2 parameters or 5 parameters when the function
prototype requires 3.


It's an undefined behaviour. The result can be anything at all.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2

P: n/a
On Mon, 26 Jul 2004, Yuan Zhong wrote:
Hi, Can someone please explain me what's going on during a call to a
function. Specifically, I wanted to know what's going on in Stacks.
There is no one answer to this for C language. There is flexibility in how
each person creates their C compiler. You could ask how is it implemented
on your compiler. For that question you'll need to find a newsgroup that
deals with your specific compiler. For example, there are newsgroups for
the gcc compiler where you could ask this.
Why is it ok to pass only 2 parameters or 5 parameters when the function
prototype requires 3.


No. Any good compiler will not compile the code if you do this. The only
exception is when the function prototype has ... in it. For example,
printf prototype is:

int printf(const char *, ...);

This means it can take one or more input parameters. How many input
parameters depends on the format of the first parameter. For example, if
the first parameter is "%d %d %d" then it will need three more inputs (one
for each %d).

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #3

P: n/a
Yuan Zhong wrote:
Hi, Can someone please explain me what's going on during a call to a
function. Specifically, I wanted to know what's going on in Stacks.
In some compilers, the parameters are "pushed" onto a stack. Other
implementations pass parameters via registers.

Upon a return, some implementations place the return value on the
stack. The popping of arguments off of the stack may be the
responsibility of the calling protocol or the function's protocol.

Some implementations can pass return values via registers.

Why is it ok to pass only 2 parameters or 5 parameters when the function
prototype requires 3.

thanks.

Show the code.

In C++, parameters can be assigned default values. So that if the
parameter is not supplied by the caller, the function receives
the default value. But, that is an issue for another newsgroup.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #4

P: n/a
Thomas Matthews <Th****************************@sbcglobal.net> wrote:
: Yuan Zhong wrote:
:> Hi, Can someone please explain me what's going on during a call to a
:> function. Specifically, I wanted to know what's going on in Stacks.
:>
: In some compilers, the parameters are "pushed" onto a stack. Other
: implementations pass parameters via registers.

: Upon a return, some implementations place the return value on the
: stack. The popping of arguments off of the stack may be the
: responsibility of the calling protocol or the function's protocol.

: Some implementations can pass return values via registers.
:> Why is it ok to pass only 2 parameters or 5 parameters when the function
:> prototype requires 3.
:>
:> thanks.
:>
: Show the code.

This is code I tested under gcc3.3.2
It compiles successfully whether I pass one parameter or more than two.
Any explainations? Thanks.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

char* cmp_string(char *a, char *b)
{
return (strlen(a)>strlen(b)?a:b);
}
int main(void)
{
char *a="hello", *b="hello, world\n";
char *(*f)();
f=&cmp_string;

printf("the longer one is %s", f(a,b,"hello","blah blah"));
printf("the longer one is %s", f(a));

return 0;

}

: In C++, parameters can be assigned default values. So that if the
: parameter is not supplied by the caller, the function receives
: the default value. But, that is an issue for another newsgroup.
: --
: Thomas Matthews

: C++ newsgroup welcome message:
: http://www.slack.net/~shiva/welcome.txt
: C++ Faq: http://www.parashift.com/c++-faq-lite
: C Faq: http://www.eskimo.com/~scs/c-faq/top.html
: alt.comp.lang.learn.c-c++ faq:
: http://www.raos.demon.uk/acllc-c++/faq.html
: Other sites:
: http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #5

P: n/a
Yuan Zhong wrote on 26/07/04 :
This is code I tested under gcc3.3.2
It compiles successfully whether I pass one parameter or more than two.
Any explainations? Thanks.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

char* cmp_string(char *a, char *b)
{
return (strlen(a)>strlen(b)?a:b);
}

int main(void)
{

char *a="hello", *b="hello, world\n";
char *(*f)();
This is a dangerous game (a sin actually). It's a function declaration
(unspecified number and types of parameters). Should be a prototype:

char *(*f)(char *a, char *b);

Now, the compiler will whine.

f=&cmp_string;

printf("the longer one is %s", f(a,b,"hello","blah blah"));
printf("the longer one is %s", f(a));

return 0;

}


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #6

P: n/a
On Mon, 26 Jul 2004 16:41:37 +0000, Yuan Zhong wrote:
Thomas Matthews <Th****************************@sbcglobal.net> wrote:
: Yuan Zhong wrote:
:> Hi, Can someone please explain me what's going on during a call to a
:> function. Specifically, I wanted to know what's going on in Stacks.
:>
: In some compilers, the parameters are "pushed" onto a stack. Other
: implementations pass parameters via registers.

: Upon a return, some implementations place the return value on the
: stack. The popping of arguments off of the stack may be the
: responsibility of the calling protocol or the function's protocol.

: Some implementations can pass return values via registers.
:> Why is it ok to pass only 2 parameters or 5 parameters when the function
:> prototype requires 3.
:>
:> thanks.
:>
: Show the code.

This is code I tested under gcc3.3.2
It compiles successfully whether I pass one parameter or more than two.
Any explainations? Thanks.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

char* cmp_string(char *a, char *b)
{
return (strlen(a)>strlen(b)?a:b);
}
int main(void)
{
char *a="hello", *b="hello, world\n";
char *(*f)();

a function with the prototype char *func();
is a function that takes _any_ number of parameters, and is
very diffrent from char *func(void);


Nov 14 '05 #7

P: n/a

On Tue, 27 Jul 2004, [iso-8859-1] Nils O. Selåsdal wrote:

On Mon, 26 Jul 2004 16:41:37 +0000, Yuan Zhong wrote:

This is code I tested under gcc3.3.2
It compiles successfully whether I pass one parameter or more than two.
Any explainations? Thanks.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
Use <> angle brackets around the standard headers.
#include <stdio.h>
[...]
char* cmp_string(char *a, char *b)
For future reference: '(const char *a, const char *b)' would have
been more appropriate, since this function doesn't need to modify
the things to which its parameters point.
{
return (strlen(a)>strlen(b)?a:b);
}
int main(void)
{
char *a="hello", *b="hello, world\n";
char *(*f)();


a function with the prototype char *func();
is a function that takes _any_ number of parameters, and is
very diffrent from char *func(void);


A function with the declaration 'char *func();' has /no/ prototype,
and is a function that takes a currently /unspecified/ number of
parameters. It is compatible with, but not precisely equivalent to,
'char *func(void);'.

In other words,

char *func();
func(a);
func(a,b);

is guaranteed to be invalid C code, but the compiler is not actually
required to diagnose the error. Many compilers /do/ diagnose such
errors, if at all possible. In any event, my example above produces
undefined behavior (in one of the two function calls above, depending
on the real, actual type of 'func').

For the OP's benefit: Declare every variable with the most specific
type you can, and you'll never have to worry about these nitpicky
areas of the language.

HTH,
-Arthur

Nov 14 '05 #8

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message
news:Pi**********************************@unix44.a ndrew.cmu.edu...
A function with the declaration 'char *func();' has /no/ prototype,
and is a function that takes a currently /unspecified/ number of
parameters. It is compatible with, but not precisely equivalent to,
'char *func(void);'.

In other words,

char *func();
func(a);
func(a,b);

is guaranteed to be invalid C code, but the compiler is not actually
required to diagnose the error. Many compilers /do/ diagnose such
errors, if at all possible. In any event, my example above produces
undefined behavior (in one of the two function calls above, depending
on the real, actual type of 'func').


ITYM in _at least_ one of the two function calls above.

Alex
Nov 14 '05 #9

P: n/a
>> In other words,

char *func();
func(a);
func(a,b);

is guaranteed to be invalid C code,


Not necessarily.

Function "func()" could actually be implemented in such a way that
it will be perfectly ok to call it with a variable number of parameters.

Since parameters are pushed onto the stack by the caller in C, much
can be done. ;-)

Of course, this is not good practice.
If you intend to write a function with variable parameters, use the
proper prototype using "..." where it is needed.

But still, your claim that the above code was *guaranteed* to be invalid
was a bit exxagerated.
Nov 14 '05 #10

P: n/a
Guillaume wrote:

Since parameters are pushed onto the stack by the caller in C, much
can be done. ;-)


No; the argument values are transmitted from the call
to the called function by a flock of carrier pigeons, as
described in RFC 2549.

(In other words, the C Standard requires that the
function's formal parameters somehow acquire the values
of the argument expressions, but says nothing at all
about how this is accomplished. Some machines use a
stack, some use registers, some use a mixture of both,
and some use Black Magic.)

--
Er*********@sun.com

Nov 14 '05 #11

P: n/a
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> spoke thus:
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.


Just curious, why the sudden switch to MIME-encoded posts?
Unintentional, perhaps?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #12

P: n/a

On Tue, 27 Jul 2004, Christopher Benson-Manica wrote:

Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> spoke thus:
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.


Just curious, why the sudden switch to MIME-encoded posts?
Unintentional, perhaps?


Apparently so. :( I checked some other recent posts of mine, and
didn't find the "multipart/mixed" type. But CMU Computing Services
was updating the system recently, so maybe it was related. Seems
like it's fixed now.

Thanks for the heads-up, though; I wouldn't have noticed otherwise.

-Arthur

Nov 14 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.