A C Puzzle
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 } |
chevron_right
filter_none
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"); } |
chevron_right
filter_none
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
Recommended Posts:
- A C/C++ Pointer Puzzle
- A C/C++ Function Call Puzzle
- A C Programming Language Puzzle
- A nested loop puzzle
- time.h header file in C with Examples
- __builtin_inf() functions of GCC compiler
- Count substrings that contain all vowels | SET 2
- How can we use Comma operator in place of curly braces?
- Basic Code Optimizations in C
- Constants vs Variables in C language
- Sum of an array using MPI
- Difference between pointer to an array and array of pointers
- dot (.) operator in C/C++
- Difference between while and do-while loop in C, C++, Java



