Why is std :: cout << main << std :: endl prints 1?

 #include <iostream> int main(){ std::cout << main << std::endl; return 0; } 

Why does it print 1 on the command line?

+5
source share
1 answer

Your program is poorly formed. N4140:

§3.6.1 / 3 The main function should not be used in a program. [...]

If we pretend that the program was not poorly formed, the most likely explanation is the standard conversion:

§4.3 / 1 The value of a function of type T can be converted to prvalue type "on T " The result is a pointer to a function.

§4.12 / 1 The value of arithmetic, an enumerated enumeration, a pointer, or a pointer to a member type can be converted to a prvalue of type bool . a null value, a null pointer value, or a null element pointer value is converted to false ; any other value is converted to true . For direct initialization (8.5), prvalue of type std::nullptr_t can be converted to prvalue of type bool ; The resulting value is false .

If you insist on this, try:

 std::cout << (void*)main << std::endl; 
+5
source

Source: https://habr.com/ru/post/1213916/


All Articles