Why does cout<<main;
printing 1
on my computer? I thought it would print the address of main. But when I use printf("%x",main);
I get different output.
Edit:
I tried std::cout
for other functions. I'm getting 1
for every case.
Why does cout<<main;
printing 1
on my computer? I thought it would print the address of main. But when I use printf("%x",main);
I get different output.
Edit:
I tried std::cout
for other functions. I'm getting 1
for every case.
It is nothing but undefined behavior
. It is an example of a code whose behavior is unpredictable.
void foo(){};
cout << foo << endl;
A function pointer will be converted to bool
, unless use it like this:
cout << reinterpret_cast<void*>(foo) << endl;
EDIT: this is undefined behavior, main
can not be used like other function pointers.
C++11(ISO/IEC 14882:2011) §3.6.1: Main function
3 The function main shall not be used within a program. The linkage (3.5) of main is implementation-defined. A program that defines main as deleted or that declares main to be inline, static, or constexpr is ill-formed. The name main is not otherwise reserved. [ Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. —end example ]