retagged by
14,462 views
41 votes
41 votes

Consider the following function written in the C programming langauge :

void foo(char *a)
 {
    if (*a && *a != ' ')
      {
       foo(a+1);
       putchar(*a);
      }
}    

The output of the above function on input "$ABCD \ EFGH$" is

  1. $ABCD \ EFGH$
  2. $ABCD$
  3. $HGFE \ DCBA$
  4. $DCBA$
retagged by

4 Answers

Best answer
58 votes
58 votes

Answer D as priority of $!=$ is greater than that of $\&\&$ in C. The execution happens as:

if ((*a) && (*a != ' '))

So, the if breaks either when $*a = 0$ (not '$0$' but ASCII $0$ or null character '\0'), or when $*a =$' '.

So, the recursive call goes like

$\text{'A' - 'B' - 'C' - 'D' -' '}$ (breaks) and then starts outputting

$DCBA$

edited by
2 votes
2 votes

as given in condition :  if *a = ' ' then it will not proceed further..hence we have to take only till ABCD ,

forget about EFGH.
aslo, it is tail recursion , means , your last called function's full body will execute first. Hence it will print DCBA.

Answer:

Related questions

59 votes
59 votes
4 answers
2
go_editor asked Sep 28, 2014
19,166 views
Consider the following function.double f(double x){ if( abs(x*x - 3) < 0.01) return x; else return f(x/2 + 1.5/x); }Give a value $q$ (to $2$ decimals) such that $f(q)$ wi...
88 votes
88 votes
5 answers
4