I agree, it is quite stupid. Though I can see a C teacher mentioning it, just to show that a lot of functions have return values, and that sometimes unexpectedly, expressions return a non-void value.
Originally Posted by Salem
In case the OP is wondering as to the reasoning, the reason being is that printf(), while being a function who's return value is often ignored, DOES NOT have a void return, it returns an int, like most functions in the std libs.
On failure printf() returns a value less than 0 (negative).
On success printf() returns the number of characters printed.
So if you print some chars, ie.
You might end up with some output similar to:
int a = printf("Hello\n");
The sixth char is because of the linebreak escape character.
In C any NON-ZERO VALUE is true as far as conditionals are concerned, this includes any number, such as ... 6. So printf("Hello") returns 5, 5, not being equal to 0, means the IF conditional is TRUE, the exclamation is the logical NOT operator, making the total evaluation FALSE, so the IF is not true, it goes into the ELSE, and prints "World" - another method could be:
Which would have the same output.
if (printf("Hello")) // printf returns 5, 5 != 0, therefore condition is TRUE
As you may or may not be able to tell, even though the function is inside the IF conditional as so, it still executes.
Hope that clears it up if there were any ambiguities regarding the rationale behind why that works.
That is a very big thing with C. Any NON-ZERO VALUE is TRUE.
For example, in the old days, before C99, before they replaced the NULL macro to be a void pointer to address 0, people simply compared pointers to 0 in order to test if they were null.
Old style would be:
char* ptr_to_string = 0;
if (ptr_to_string != 0)
;// do some magic
printf("PTR IS NULL\n"); // error case, which will be printed
ptr_to_string = (char*)malloc(512); // Now "ptr_to_string" is pointing to an address on the heap, a NON-ZERO ADDRESS, it now evaluates to true.
printf("Pointer is NOT null!\n"); // Which will be printed
printf("PTR IS NULL\n"); // Not printed anymore, the pointer is not null.
; // Perfectly valid, if "ptr_to_string" is NULL (0), it's false, if it's an address (any address), it's true.