Originally Posted by
Salem
I see the students suffering at the hands of a "teacher" who thinks teaching stupid programming tricks is somehow worthwhile.
Here, since you're not going to learn anything where you are, a pointless freebie
Code:
if( !printf("hello") )
printf("hello");
else
printf("world");
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.
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.
Code:
int a = printf("Hello\n");
printf("%i\n", a);
You might end up with some output similar to:
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:
Code:
if (printf("Hello")) // printf returns 5, 5 != 0, therefore condition is TRUE
printf("World");
else
printf("Hello");
Which would have the same output.
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.
EDIT:
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:
Code:
char* ptr_to_string = 0;
if (ptr_to_string != 0)
;// do some magic
else
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.
if (ptr_to_string)
printf("Pointer is NOT null!\n"); // Which will be printed
else
printf("PTR IS NULL\n"); // Not printed anymore, the pointer is not null.
Not much has changed in terms of use, but people rarely compare pointers to 0 anymore, simply they compare them to NULL, or don't comare them to anything, ie.
Code:
if (ptr_to_string)
; // Perfectly valid, if "ptr_to_string" is NULL (0), it's false, if it's an address (any address), it's true.