Why output is 1 ? | SoloLearn: Learn to code for FREE!


Why output is 1 ?



7/12/2020 4:05:05 PM


22 Answers

New Answer


Since main is a function, it too has an address. The printf statement is converting that address to an integer. The behavior is different in C and C++ when the function is printed to standard output stream but behaves in the same way if used in a boolean expression. if (main) { puts("main"); }


Yup thanks 😊


Ok thanks ~ swim ~


~ swim ~ https://code.sololearn.com/coCkNSX0Oq3g/?ref=app Why it gives random value Why not 1


main is a function, if you just cout function name it is coerced to bool (true) and true is convertible to 1. If you want to see output in terms of true or false then write like this cout << boolalpha << main;


I have updated my comment, please read it again.


Why just main....just create any function...with returning value of int and then print it....it will always print 1 ....you can try it. ~ swim ~ has already explained the behaviour!


it is giving 1 because the program executed succesfully and to indicate that it represents it by a number which in binary terms means " on and true". Hope i said right


#include<iostream.h> Using namespace std; Int main(){ Int variable=1; Cout<<"variable"; Return 0; } #output- 1.


⸙ƵØ¥₳⸙ #include <stdio.h> int main() { printf("%d",main); return 0; } Bcz the program allocates stack space, and never moves anything to that address, so whatever was there previously is used. In other words...... computers have finite storage capacity, automatic variables will typically be held in storage elements (whether registers or RAM) that have previously been used for some other arbitrary purpose. If such a variable is used before a value has been assigned to it, that storage may hold whatever it held previously, and so the contents of the variable will be unpredictable.


In case of printf what is printed is the adress of the main function or the pointer to the main function. In case of cout (C++) the use of main is undefined behaviour and the adress is is casted to bool. Just as ~ swim ~ explained.


Why use using namespace STD in c++ Plzz explained


⸙ƵØ¥₳⸙ in C when ever initialize a variables garbage value was stored in that place. Try this you can understand better way Int a; Printf("%d",a); Output was some garbage value. Until we assign a value that variables has garbage value.


Agree with swim


First, main() is a function. Second, function name used as an address of memory space occupied. Next, read this one, the standard of C++: Addresses always evaluate as true!!!! Problem: I received the following warning: the address of 'ThisSubCommand' will always evaluate as ‘true’ [-Waddress] Solution: The compiler issues a warning when a pointer address by itself is compared in an if statement. You might have wanted to test whether the string is a null string; that is, to test if the first element of the string is ‘\0’. In this case, you must check the contents of the first element. Change the code to use an if statement to compare the first element of the structure, or use the memcmp function to do the comparison. For example, fix this code: if (ThisSubCommand) { Copy code Corrected code: if (ThisSubCommand[0] != '\0')


your statement cout<<function_name; is interpreted by c++ compiler as If (*ptr !=0) or if *ptr is not equal to NULL pointer. This is the implication that the function name itself represents the memory address occupied in the memory block. the interpretation of cout<<main is similar to the notation of If clause If (a>b) return a; else return b; can be wrote as: cout<< a>b ? a:b; evaluate as boolean true or false.


Deitel 7th C++: Chapter 17. Another overloaded ios member function— operator void * —converts the stream to a pointer, so it can be tested as 0 (i.e., the null pointer) or nonzero (i.e., any other pointer value). When a pointer value is used as a condition, C++ interprets a null pointer in a condition as the bool value false and interprets a non-null pointer as the bool value true .


main can not be Null or False That's why it's > 1️⃣


Why just main....just create any function...with returning value of int and then print it....it will always print 1 ....you can try it. ~ swim ~ has already explained the behaviour!


It so sample