The Wayback Machine - https://web.archive.org/web/20210501060825/https://www.geeksforgeeks.org/a-c-puzzle-2/
Skip to content
Related Articles

Related Articles

A C Puzzle
  • Difficulty Level : Easy
  • Last Updated : 21 Jun, 2018

What code to write in place of “// your code” so that the below code prints 20.




#include <stdio.h>
int f();
  
int main()
{
    int a = 0;
    f();
    printf("%d",a);
    return 0;
}
  
int f()
{
   // your code
}

Output:

20 

We strongly recommend you to minimize your browser and try this yourself first

This question seems to be a trick question, as it is not possible to update local variable in a function without sending it, we can’t make value of ‘a’ as 20 but for outputting 20 we can write function f as below –




#include <stdio.h>
int f();
  
int main()
{
    int a = 0;
    f();
    printf("%d",a);
    return 0;
}
   
int f()
{
    printf("2");
}

2 will be printed by f() and 0 will be printed by a

Thanks to Utkarsh Trivedi for suggesting above solution.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :