retagged by
5,084 views
8 votes
8 votes

Consider the following $\text{C}$ program. Assume parameters to a function are evaluated from right to left.

#include <stdio.h>

int g( int p) { printf("%d", p); return p; }

int h(i nt q) { printf("%d", q); return q; }

void f (int x, int y) {

g(x);

h(y);

}

int main() {

f (g(10), h(20));

}

Which one of the following options is the CORRECT output of the above $\text{C}$ program?

  1. $20101020$
  2. $10202010$
  3. $20102010$
  4. $10201020$
retagged by

3 Answers

10 votes
10 votes

$\underline{\textbf{Let's understand diagrammatically}} :$

 

$\text{As its mentioned $\textbf{"parameters to a function are evaluated from right to left"}$}.$

$\text{So, h() will be evaluated first, and it will print 20 first then g() will be evaluated and it will print 10.}$

 

 $\text{Now as g() and h() both function will return 10 and 20 respectively, so now $\textbf{f(10, 20)}$ will be called and from there,}$

$\text{ then again g() and h() will be called and 10 and 20 will be printed respectively.}$  

$\newline$

$\boxed{ \color{Red}\underline{\textbf{Output}}  \color{Black}\textbf{ : 20 10 10 20} }$   $\text{(Option A)}$

$\newline$


$\newline$

$\underline{\textbf{NOTE}} :$

"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".

$\newline$

$\underline{\text{For example}}:$  

               $\boxed{ \text{ fun(A(), B(), C()) } }$  $\text{for this fun() we can't say order of evolution is always either from Right to Left }$

$\text{or Left to Right. Its totally Unspecified according to}$ ISO - C99 Standard.

$\newline$

$\text{The possible order of evolution of A(), B(), C() can be: }$ 

$\text{ A(), B(), C() }$

$\text{ A(), C(), B()  }$

$\text{ B(), A(), C() }$

$\text{ B(), C(), A() }$

$\text{ C(), A(), B() }$

$\text{C(), B(), A()}$

$\newline$

$\underline{\text{References}} :$

              1. Order of evaluation in C

              2. ISO/IEC 9899:1999 (Chapter 6.5)

edited by
0 votes
0 votes

As mentioned in the question, the parameter evaluation is assumed to be from right to left. So following is the sequence of function execution :

h(20) : prints 20 and assigns 20 to y.

g(10) : prints 10 and assigns 10 to x

g(x) , ie. g(10) , prints 10 and returns 10 which is not catched.

h(y) ie. h(20) prints 20 and returns 20 which is not catched. 

on display user will see 20101020, ie. A

Answer:

Related questions

3.2k
views
3 answers
7 votes
Arjun asked Feb 16
3,155 views
Consider the following $\mathrm{C}$ function definition.int f X(char * a) { char * b = a; while (*b) b ++; return b - a; }Which of the following ... in main (), the function call $\mathrm{fX}(\mathrm{c})$ will always return a value
4.1k
views
3 answers
6 votes
Arjun asked Feb 16
4,101 views
​​​​​What is the output of the following $\text{C}$ program?#include <stdio.h> int main() { double a[2]=20.0,25.0,* p,* q; p=a ; q=p+1 ; printf("%d,%d", (int) (q-p),( int)(* q- * p)); return 0;$4,8$1,5$8,5$1,8$
316
views
1 answers
2 votes
GO Classes asked Apr 30, 2022
316 views
What will be printed by the following program?#include<stdio.h> int func(int n, int * fg) { int t, f; if (n <= 1) { *fg = 1; return 1; } t = func(n - 1, fg); f = t ... = t; return f; } int main() { int x = 15; printf("%d\n", func(5, &x)); }
468
views
1 answers
3 votes
GO Classes asked Apr 30, 2022
468 views
#include<stdio.h> int test(int *a, int *b) { int c = *a-*b; if (c<0) return 0; else return (1 + test(&c, b)); } void main() { int x = 15; ... }$2$3$4$Run time error since we can not pass the address of local variable c in the function test.