if i write
if(printf("C is Wonderful")
{
} It worked.
But if i write
if(cout<<"C is wonderful")
{
} Can anyone tell me whats the reason behind it ?
if i write
if(printf("C is Wonderful")
{
} It worked.
But if i write
if(cout<<"C is wonderful")
{
} Can anyone tell me whats the reason behind it ?
Short answer: they behave differently because of return type and semantics. printf returns an int (number of characters printed, or a negative value on error), so using it directly in if(...) tests that integer. The insertion expression cout << ... returns a reference to the stream object; that reference is converted to a boolean context that reflects the stream’s error state. Both can compile and be used in a condition, but they mean different things. (Also note the posted printf line as shown is missing a ) — that would not compile, as pointed out.)
For robust code check the actual success/failure instead of relying on truthiness. Example checks:
int written = printf("message\n");
if (written < 0) {
perror("printf failed");
} cout << "message" << flush;
if (!cout) {
cerr << "cout failed\n";
} A few practical points tied to earlier replies: is right to call out the different return types; is also correct that streams can be tested for an error state (older standards used the safe-bool/void* idiom; modern C++ uses an explicit operator bool). Because both stdio and iostreams buffer output, an I/O error might only be discovered at flush time — so check at logical flush points or call fflush / flush before testing. For C++ consider enabling stream exceptions (cout.exceptions(...)) or explicitly checking fail()/bad() when error handling matters.
Jump to Post— prog-bman 4What do you mean? You didn't finish your thought there.
Jump to Post— Ancient Dragon 5,243garbage in, garbage out. printf() is a function that returns an int which is the number of characters printed. cout is a c++ class that returns a reference to ostream object. printf() and cout are two entirely different things which do something similar but have different return values. Putting cout …
What do you mean? You didn't finish your thought there.
garbage in, garbage out. printf() is a function that returns an int which is the number of characters printed. cout is a c++ class that returns a reference to ostream object. printf() and cout are two entirely different things which do something similar but have different return values. Putting cout in an if statement like that is meaningless.
Your first one couldn't have worked in the first place as you are missing a ) and wouldn't compile
yea i would think it was because printf() is a function that reurns a value and therefore you have something for an argument within the if statement, whereas cout is a just a 'class' with an output operator as ancient dragon explained and therefore doesnt provide an argument
>whereas cout is a just a 'class' with an output operator as ancient
>dragon explained and therefore doesnt provide an argument
There's nothing wrong with using the result of cout's << operator in a conditional statement as you seem to think. All standard streams have a conversion to void* which can be used as the "argument" of a conditional statement. However, the result of cout's << operator is rarely surprising, and will usually only enter an error state when there's some catastrophic failure, so there's no point in testing it.
Now, operator>> with cin is different because it's entirely likely that a request for input could fail, which would place the stream in an error state and conversion to void* would give you a null pointer. In that case it makes a lot of sense to use an if statement:
if ( cin>> x ) {
// Use x
}
else {
// Error!
} >Putting cout in an if statement like that is meaningless.
On the other hand, say you have a stream into a temporary device such as a thumb drive or floppy. If the media is suddenly removed before or during a write operation then the output will fail, and the test makes sense. If cout is redirected to be used in such a way then it's far from meaningless.
By the way, cout is not a class, it's an instance of a class. There's a big difference, but a lot of people seem to mix up the two.
Thanx Buddies I got ur points
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.