Day#21 'C' Coding Challenge

Day#21 'C' Coding Challenge

What is the output of the below code?

#include<stdio.h> int sum(int,int); int k; void main() { int (*ptr)() = sum; (*ptr)(); printf("%d",k); } int sum(int a, int b) { a = b = 3; k = a + b; return k; }

-------------------------------------------------------------

The code tests the below scenarios:

1. Function pointer
2. Function definition and declaration
3. Global variables

------------------------------------------------------------

ptr is a function pointer.
int (*ptr)() = sum;

With (*ptr) (), sum function is called
(*ptr)();

The sum function gets executed, 
a,b are local variables, k is global variable
k holds the value 6
with return k statement, 6 is returned and is printed.

Post a Comment

0 Comments