In this document, i have briefly mentioned about the calling convention of the function whether the arguments are passed from left to right and right to left, and how to solve if the expressions are passing to the functions.
For more : http://comsciguide.blogspot.com/
1. Also See more posts : www.comsciguide.blogspot.com
Also See more posts : www.comsciguide.blogspot.com
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);
2. Also See more posts : www.comsciguide.blogspot.com
Also See more posts : www.comsciguide.blogspot.com
In this call, it doesn’t 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, C’s 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, a’s 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.
3. Also See more posts : www.comsciguide.blogspot.com
Also See more posts : www.comsciguide.blogspot.com
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 printf() function, it accepts variable number of arguments
even though if it doesn’t matches in the format specifiers and
variables listed in printf().
4. Also See more posts : www.comsciguide.blogspot.com
Also See more posts : www.comsciguide.blogspot.com
For example :
printf(“%d%d%d”,a,b);
printf(“%d%d”,a,b,c);
It doen’t 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.
Also see :
1. Programs those work in C but not in C++
2. Difference between malloc(),calloc(),realloc()
3. Dynamic memory allocation (Stack vs Heap)
4. 3 ways to solve recurrence relations