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

+24

Why output is 1 ?

https://code.sololearn.com/cN5901EclAbV/?ref=app

c++

7/12/2020 4:05:05 PM

⸙ƵØ¥₳⸙

22 Answers

New Answer

+25

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"); }

+17

Yup thanks 😊

+15

Ok thanks ~ swim ~

+14

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

+12

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;

+6

I have updated my comment, please read it again.

+4

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!

+2

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

+2

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

+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.

+1

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.

+1

Why use using namespace STD in c++ Plzz explained

+1

⸙ƵØ¥₳⸙ 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.

+1

Agree with swim

+1

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')

+1

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.

+1

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 .

0

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

0

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!

-2

It so sample