I came across below example in w3schools, while learning about javascript closures. I was wondering if this could be achieved in C programming, since we have similar function pointers in C. However, I was not able to completely achieve this with simple pointers to function implementation. Can someone please give it a try? Redirect me if it has already been asked.
var add = (function () {
var counter = 0;
return function () {return counter += 1;}
})();
add();
add();
add();
// the counter is now 3