Can anyone explain this code | SoloLearn: Learn to code for FREE!


Can anyone explain this code

#include <stdio.h> int func(){ return (printf("hello world")); } int main() { printf("%d\n",sizeof(func)); return 0; }

11/14/2019 11:35:27 AM


6 Answers

New Answer


Every function has an address so &func will have size 8


Preity It is invalid to call sizeof with function( sizeof(func) is an invalid call) although it is not an error you will get warning. And it is likeky to get convert to 1 in C and true(bool) in C++ probably that is why the output is 1


~ swim ~ Also can you tell why &func is giving 8?


Avinesh &func is an address of the function i.e assignable to pointer to a function and sizeof pointer is 8


Thank you ~ swim ~ Saurabh B


Also note using name of function alone does not always convert/decays to bool. It depends on the context/usage typedef void (*pFunc) (); void fun() { puts("hello"); } pFunc pf1 = fun; // name of function decays to address of the function not to bool pf1(); // or (*pf1)(); prints "hello" void test (bool b) { if(b) puts("true"); else puts("false"); } test (fun); // function name fun decays to bool here and prints "true"