43 43 votes What is the value printed by the following C program? #include<stdio.h> int f(int *a, int n) { if (n <= 0) return 0; else if (*a % 2 == 0) return *a+f(a+1, n-1); else return *a - f(a+1, n-1); } int main() { int a[] = {12, 7, 13, 4, 11, 6}; printf("%d", f(a, 6)); return 0; } $-9$ $5$ $15$ $19$ Algorithms gatecse-2010 algorithms recursion identify-function normal + – go_editor 17.1k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply krish__ commented Nov 22, 2017 reply Follow flag @kenzou, The program won't compile with parenthesis used in the initializer. Please change it to "{ }" 1 1 replyShare Arjun commented Aug 25, 2022 reply Follow flag Fixed now. 0 0 replyShare Please log in or register to add a comment.
Best answer 37 37 votes Suppose $int$ array takes $4$ bytes for each element and stored at base address $100$. Follow below image. Red color shows the return value. So, $15$ is the answer. Correct Answer: $C$ Rajesh Pradhan answered Nov 11, 2016 • edited Nov 6, 2021 by Kiyoshi Rajesh Pradhan comment Share Follow See all 8 Comments 8 8 Comments reply Arjun commented Nov 11, 2016 i edited by Arjun Aug 25, 2022 reply Follow flag In C leaglly it is possible to have 2 byte for "int". But there does not make any sense to have an int value which cannot go beyond $2^{16}-1 = 65535 .$ So, please assume 4 bytes whenever we use int in C. Using 2 bytes for int is like making fun of C :( 12 12 replyShare Rajesh Pradhan commented Nov 11, 2016 i edited by Rajesh Pradhan Nov 12, 2016 reply Follow flag Thanks a lot @arjun sir . I will keep this in my mind in future. I will update this IMG tomorrow.n Edited @Arjun Sir 1 1 replyShare Shiva Sagar Rao commented Nov 23, 2020 reply Follow flag Follow below image. Red color shows the return value. There is no image in the answer. 0 0 replyShare Kiyoshi commented Jun 7, 2021 i edited by Kiyoshi Jun 7, 2021 reply Follow flag @ Arjun Sir, there does not make any sense to have an int value which cannot go beyond 31267 isn’t it cannot go beyond 32767. we have 2 bytes means, = 2*8 (16 bits) so from 16 bits we can have range from- $- 2^{n-1}$ to $+2^{n-1}$ -1 So it’s value will be , -32768 to +32767 Anyone correct me if i’m wrong..or missing something 0 0 replyShare Thadymademe commented Aug 25, 2022 reply Follow flag @Arjun Sir but in C language size of int is compiler dependent. 0 0 replyShare Arjun commented Aug 25, 2022 reply Follow flag Does that matter here? 0 0 replyShare Thadymademe commented Aug 25, 2022 reply Follow flag No @Arjun Sir but in general it matters right like of a question if asked that what is the size of int data type in C language ?? 0 0 replyShare Abhrajyoti00 commented Dec 16, 2022 reply Follow flag @abir_banerjee Such a question will never be asked for sure. 0 0 replyShare Please log in or register to add a comment.
39 39 votes It will print $12 + ( 7 - (13 - (4 + (11 - ( 6 + 0)))))$ $\quad = 12 + (7 - (13 - ( 4 + ( 11 -6)))))$ $\quad= 12 + 7 - 13 + 9$ $\quad= 15$ gatecse answered Oct 1, 2014 gatecse comment Share Follow See all 2 Comments 2 2 Comments reply pavansan commented Jan 4, 2025 reply Follow flag @gatecse sir can u pls tell me how this addition and subtraction works here i am not getting 0 0 replyShare Amjad. commented Apr 15, 2025 reply Follow flag @pavansan add or sub which are present inside the parathesis, do this layer by layereg : 6+0 is done first and then 11 - 6 and then ... 0 0 replyShare Please log in or register to add a comment.
18 18 votes Answer : Option C) 15 tvkkk answered Jul 9, 2017 tvkkk comment Share Follow See 1 comment 1 1 comment reply indra kumar sahu commented Oct 18, 2020 reply Follow flag nice explaination 0 0 replyShare Please log in or register to add a comment.
5 5 votes it is C. 15. Gate Keeda answered Oct 1, 2014 Gate Keeda comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote Here is the explanation !! ritiksri8 answered May 10, 2024 ritiksri8 comment Share Follow 0 reply Please log in or register to add a comment.