Function arguments

A

Amit Sharma

Hi,
I wanted to know when we call function with argument, Does the
arguments stored in stack always or it compiler specific and could be
stored in queue

e.g. Does this program's output depends upon the compiler specific or
always would be b a

#include<stdio.h>

int a()
{
printf("a\t");
return 0;
}
int b()
{
printf("b\t");
return 0;
}
void c(int a,int b)
{
}
int main()
{
c(a(),b());
return 1;
}


Thanks,
Amit
 
C

Chris Dollin

Amit said:
Hi,
I wanted to know when we call function with argument, Does the
arguments stored in stack always or it compiler specific and could be
stored in queue

They can be stored however the implementor likes. A common choice, on
machines where it's available, is to store at least the first few
arguments in registers.
c(a(),b());

The arguments of a function call can be evaluated in whatever order
the compiler (writer) finds convenient and effective.
 
D

Dan Pop

In said:
I wanted to know when we call function with argument, Does the
arguments stored in stack always or it compiler specific and could be
stored in queue

Why does it matter to you?
e.g. Does this program's output depends upon the compiler specific or
always would be b a

#include<stdio.h>

int a()
{
printf("a\t");
return 0;
}
int b()
{
printf("b\t");
return 0;
}
void c(int a,int b)
{
}
int main()
{
c(a(),b());
return 1;
}

I see no connection between your first question and the second one,
although the second was supposed to exemplify the first.

The output of the program is not affected by how and where the function
arguments are stored, but it is affected by the order in which the
function arguments are evaluated, which is something completely different.
The order of evaluation is unspecified, therefore the program can output
either "a\tb\t" or "b\ta\t". Or nothing at all, because the last line of
output is not newline terminated.

BTW, what is the effect of returning 1 from main?

Dan
 
K

Kevin D. Quitt

Hi,
I wanted to know when we call function with argument, Does the
arguments stored in stack always or it compiler specific and could be
stored in queue

Curious. I can't find the word "stack" anywhere in the C standard. So,
"no" would be the answer to the first part of the question.
 
J

Jamie Lin

The default calling convention for C is _cdecl. It means parameters are
pushed onto the stack in the reversed order (right to left) and the caller
needs to clean up the stack. There are other conventions, for example,
_stdcall is used for Win32 API functions. For your question, it is true when
you use gcc and run it on GNU/Linux systems. Since it's a default
convention, I assume other compliers will give you the same thing unless you
specify it. But again, I could be wrong in the second case.

-Jamie
 
K

Keith Thompson

Jamie Lin said:
The default calling convention for C is _cdecl. It means parameters are
pushed onto the stack in the reversed order (right to left) and the caller
needs to clean up the stack. There are other conventions, for example,
_stdcall is used for Win32 API functions. For your question, it is true when
you use gcc and run it on GNU/Linux systems. Since it's a default
convention, I assume other compliers will give you the same thing unless you
specify it. But again, I could be wrong in the second case.

First, please don't top-post. Your response should follow, not
precede, any text you quote from previous messages. (See other
articles in this newsgroup for examples.)

Second, there is no such thing as "_cdecl" in standard C, nor does C
require the existence of a stack. As far as the standard is
concerned, parameters could be pushed onto the stack in forward,
reverse, or alphabetical order, stored in registers, or sent by
carrier pigeon. What you say may (or may not; I don't know) be valid
for some particular system, but I assure you there are plenty of
systems where it isn't.
 
C

CBFalconer

Jamie said:
The default calling convention for C is _cdecl. It means parameters
are pushed onto the stack in the reversed order (right to left) and
the caller needs to clean up the stack. There are other conventions,
for example, _stdcall is used for Win32 API functions. For your
question, it is true when you use gcc and run it on GNU/Linux
systems. Since it's a default convention, I assume other compliers
will give you the same thing unless you specify it. But again, I
could be wrong in the second case.

Don't toppost. Your answer belongs after, or intermixed with, the
material to which you are replying, after snipping away anything
that is not relevant.

C has no default calling convention. _cdecl and _stdcall are
specific to some systems and are not portable. The parameter
order is not defined, and neither is the existence of a stack.
 
P

pete

Jamie said:
The default calling convention for C is _cdecl.

No.

N869
6.5.2.2 Function calls
[#10] The order of evaluation of the function designator,
the actual arguments, and subexpressions within the actual
arguments is unspecified, but there is a sequence point
before the actual call.
 
J

John Bode

Jamie Lin said:
The default calling convention for C [edit] on the Win32 platform [/edit] is
_cdecl.

Fixed that for you.
It means parameters are
pushed onto the stack in the reversed order (right to left) and the caller
needs to clean up the stack. There are other conventions, for example,
_stdcall is used for Win32 API functions. For your question, it is true when
you use gcc and run it on GNU/Linux systems. Since it's a default
convention, I assume other compliers will give you the same thing unless you
specify it. But again, I could be wrong in the second case.

-Jamie

There's a whole range of calling conventions available, many of which
do not conform to what you describe above. All The World Is *NOT*
Win32.

It depends on your platform (hardware + OS). This is less a C
language question and more a platform-specific question.
 

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

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,377
Latest member
MableYocum

Latest Threads

Top