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.1k 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 admin commented Jun 22, 2015 reply Follow flag If && had greater priority than != then also ans would have been D only. 6 6 replyShare Arjun commented Jun 22, 2015 reply Follow flag if ((*a && *a) != ' ') This will never be true as LHS is always 1 or 0 while RHS is > 1. 18 18 replyShare admin commented Jun 24, 2015 reply Follow flag ok got it thanks sir. 1 1 replyShare arun001 commented Jul 25, 2016 reply Follow flag foo(a+1) meaning? 0 0 replyShare rajan commented Aug 19, 2016 reply Follow flag @arjun sir i m not getting why priority is a issue in this question my approach is inside main call foo("ABCD EFGH") and i represent this input threw array 100 101 102 103 104 105 106 107 108 109 A B C D E F G H \0 thn base address goes to 'a' and a pointing to first cell of arrary then condition check if('A' && A != ' ') so condition is true then why u r thinking about priroty then do foo(a+1) that means they point to 101 and again condition check if('B' && B != ' ') then true then do foo(a+1) means they pointing to 102 and so on once they pointing to 104 then check condition if(' ' && ' ' != ' ') so false then goes to privious stetement which is printf then print D THEN C THEN B THEN A so output is DBCA plz tell me why r u taking priority and what is the effect of priority in such question plz verify @manojk 3 3 replyShare Arjun commented Aug 19, 2016 reply Follow flag The priority being discussed is for "&&" and "!=". 0 0 replyShare rajan commented Aug 19, 2016 reply Follow flag @arjun sir when i solve i dont think so about the priority of "&&" and "!=" and get the correct answer as i shown above in the comment . but why r u taking priority b/w "&&" and "!=" this is my question and tell me what is the role of priority in this quetion 0 0 replyShare Arjun commented Aug 19, 2016 reply Follow flag void foo(char *a) { if ((*a && *a) != ' ') { foo(a+1); putchar(*a); } } What will be the output of above code? That's all the significance of priority here. 5 5 replyShare rajan commented Aug 19, 2016 i edited by rajan Aug 19, 2016 reply Follow flag as we know bracket always evaluate first so inside if condition firstly (*a && *a) execute and gives value either 0 or 1 then this value comapre with ascii value of ' ' which is always 0 so if caondtion is can be true or false . its depend upon the what value come out from (*a && *a). is it correct ?? 0 0 replyShare Arjun commented Aug 19, 2016 reply Follow flag Not exactly, ' ' is ASCII value of space which is neither 0 nor 1. So, the codition here alwsys becomes TRUE. 9 9 replyShare rajan commented Aug 19, 2016 reply Follow flag ohh sry sir i did mistake ths ascii value of 'space' is 32 in place of space i took ascii value of null which is '0' so that means condition always true . thanq sir 2 2 replyShare neha singh commented Aug 20, 2016 reply Follow flag @Arjun sir , Can you please elaborate this priority based explanation? 0 0 replyShare Arjun commented Aug 20, 2016 reply Follow flag Clear now? Priority is not relevant for the answer here - it is mentioned just to show that answer would have been different with a different priority. 0 0 replyShare neha singh commented Aug 20, 2016 reply Follow flag ok sir. 0 0 replyShare rajankakaniya commented Jul 18, 2021 i edited by rajankakaniya Jul 18, 2021 reply Follow flag https://ideone.com/na0SgZ if ((*a && *a) != ' ') for this LHS is always 1 or 0 and ASCII of space is 32 so, if condition is always true. So, infinite loop and at the end stack overflow runtime error. 1 1 replyShare Kiyoshi commented Jun 24, 2022 i edited by Kiyoshi Jun 24, 2022 reply Follow flag Input is just "ABCD EFGH". (No stackoverflow and infinite loop) Runtime error just becasue illegal access of memory at the end by *a. 0 0 replyShare 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.