Calling Convention

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Calling Convention :

In this convention, there are mainly two important things to be


mentioned.
1) The order in which arguments are passed to function.
2) Whether the called function or calling function should clean up
the variable, when the control returns from the function.
When a function call is encountered, the actual arguments that
are to be passed to the function results in two possibilities
1) Arguments can be passed from left to right
2) Arguments can be passed from right to left
The formal arguments and local variables that we declare in the
functions are created in stack memory. When the control returns
from the function, the stack is cleared. This is done either by the
calling function or the called function, and is decided by the Calling
Convention. There are different calling conventions are available to
us. But the most used among them is Standard Calling Convention. In
this arguments are passed from right to left and stack is cleared by
the called function.
For example :
int a=1, b=2, c=3;
printf(%d %d %d , a , b , c);
For more : http://comsciguide.blogspot.com/2015/06/calling-convention.html

In this call, it doesnt matter whether the arguments are


passed from left to right or from right to left.
int a=1;

// a++ or ++a is also written as a = a + 1;

printf( %d %d %d, a, ++a, a++);


In this call, it matters about the passing of arguments. If the
arguments are passed from left to right, the output will be 1 2 2 . If
the arguments are passed from right to left, the output will be 3 3 1 .
Hence by default, Cs calling convention is from right to left.
That is First 1 is passed to the a++ expression. As from the
associativity table, we know that a++ is a postfix expression which
has associativity left to right, as value is passed to the function and
then it is incremented. And for ++a expression, the value becomes 3.
Finally the latest value of a is 3 and is passed to the function. Thus in
right to left order, 1, 3, 3 gets passed. Once Printf() function collects
them, it prints the elements in the order we have written in the
function. Thus 3 3 1 are printed.
Note :
Whenever there is an expression, Precedence and Associativity
will come. In the above expressions, a++ has highest precedence. The
highest precedence expression values are passed first and later next
expression values are passed. So as coming from right to left, the
expressions are evaluated but the values are not passed.
For more : http://comsciguide.blogspot.com/2015/06/calling-convention.html

int a=1;
Printf(%d %d, a++ , ++a);
In this case, if we see from right to left, first ++a is evaluated and a
becomes 2. But the value is not passed to the function. For a++, first
the value of a(2) is passed to printf function and incremented. Now
the incremented value is passed for ++a to the printf function. The
result will be 2 3.

Note :
If an expression contains more than one operator, then
precedence and associativity of operators comes into action.
int a = 1;
printf("%d %d" , a++, a = ++a - a);
If we see from right to left, in first expression contains combination
of operators. The priority order is ++,

-, = . So the value becomes 0.

This value is assigned to a in a++ , then it is passed and incremented.


Output will be 0 1
Note :

For more : http://comsciguide.blogspot.com/2015/06/calling-convention.html

For printf() function, it accepts variable number of arguments


even though if it doesnt matches in the format specifiers and
variables listed in printf().
For example :
printf(%d%d%d,a,b);
printf(%d%d,a,b,c);
It doent make any compilation errors.

Programs for practice :


Assume a = 1 for every printf() statement.
1) printf("%d%d%d",a,a++,++a);
2) printf("%d%d%d",++a,a,a++);
3) printf("%d%d%d",++a,a++,a);
4) printf("%d%d%d",a++,++a,a);
5) printf("%d%d%d",a,a=--a/2,a++);
6) printf("%d%d%d",a+3,a--,++a*2);
7) printf("%d%d%d",2*a+4,a-a++,a);
For solutions, Check yourself by compiling each statement.

For more : http://comsciguide.blogspot.com/2015/06/calling-convention.html

You might also like