• retagged by
22,100 views
60 60 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$

5 Answers

Best answer
77 77 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
9 9 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:
Position:
Show:

Related questions

176 176 votes
10 answers 10 answers
42.1k
42.1k views
Arjun asked Feb 14, 2017
42,080 views
Consider the C functions foo and bar given below:int foo(int val) { int x=0; while(val 0) { x = x + foo(val ); } return val; }int bar(int val) { int x = 0; while(val 0)...
141 141 votes
7 answers 7 answers
39.7k
39.7k views
go_editor asked Feb 13, 2015
39,661 views
Which one of the following well-formed formulae is a tautology? $\forall x \, \exists y \, R(x,y) \, \leftrightarrow \, \exists y \, \forall x \, R(x, y)$$( \forall x \,...
54 54 votes
5 answers 5 answers
13.6k
13.6k views
go_editor asked Feb 13, 2015
13,585 views
Let $X$ and $Y$ denote the sets containing $2$ and $20$ distinct objects respectively and $F$ denote the set of all possible functions defined from $X$ to $Y$. Let $f$ be...
47 47 votes
6 answers 6 answers
23.9k
23.9k views
go_editor asked Feb 13, 2015
23,912 views
The number of states in the minimal deterministic finite automaton corresponding to the regular expression $(0+1)^* (10)$ is _____.