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 $ABCD \ EFGH$ $ABCD$ $HGFE \ DCBA$ $DCBA$ Programming in C gatecse-2015-set2 programming programming-in-c normal recursion + – go_editor 22.0k views answer comment Share Follow Print See all 4 Comments 4 4 Comments reply shilajit commented Jul 10, 2015 reply Follow flag Since this is a recursion so characters are stored one by one on the stack and then printed right.?But if putchar(char *a) is placed before calling of the foo() then will the output be ABCD? 3 3 replyShare DarKnight commented Aug 9, 2015 reply Follow flag plzz explain someone 0 0 replyShare iwasifirshad commented May 1, 2020 reply Follow flag YES 1 1 replyShare parag parab commented Jan 1, 2024 reply Follow flag https://www.youtube.com/watch?v=6A1fVJN7dQo 1 1 replyShare Please log in or register to add a comment.
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$ Vikrant Singh answered Feb 12, 2015 • edited Jun 15, 2018 by Milicevic3306 Vikrant Singh comment Share Follow See all 19 Comments 19 19 Comments reply Show 16 previous comments Abhrajyoti00 commented Oct 27, 2022 reply Follow flag @ASNR1010 Why should there be Runtime error for illegal access? The program doesn’t even call $foo(105)$ {taking base address of *a as 100 and char as 1B}. It will successfully call till $foo(104)$ and end after popping out the remaining segments in the stack containing previous calls of $putchar(*a)$. 0 0 replyShare subhamky commented Sep 8, 2024 reply Follow flag @Arjun sirif ((*a && *a) != ' ')wont this always be true? 0 0 replyShare jack153 commented May 1 reply Follow flag agreed, condition should be always true 0 0 replyShare Please log in or register to add a comment.
33 33 votes THIS MIGHT HELP Asim Siddiqui 4 answered Nov 29, 2018 Asim Siddiqui 4 comment Share Follow 0 reply Please log in or register to add a comment.
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. AbhayPrajapati answered Apr 1, 2020 AbhayPrajapati comment Share Follow 0 reply Please log in or register to add a comment.
7 7 votes Change the answer to D. MaheshK answered Feb 21, 2015 MaheshK comment Share Follow 0 reply Please log in or register to add a comment.
3 3 votes Output:- DCBA Sharadamani_K_N answered Nov 16, 2025 Sharadamani_K_N comment Share Follow 0 reply Please log in or register to add a comment.