Issue
I have found this code in a collection of buggy code examples (http://matthieu-moy.fr/c/c_collection/) which i studied to sharpen my programming skills. You can see the code, how i executed it and the output below, as well as some experimenting i have done.
Can anyone explain this strange phenomena?
Code
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int function_returning_false() {return FALSE;}
int main() {
if (function_returning_false) {
printf("function returned true\n");
}
}
Build
$ gcc Bug_Example_7.c -o Bug_Example_7_gcc
Execution
$ ./Bug_Example_7_gcc
Output
function returned true
Conclusion
One could assume that the 'if' condition would not be true and therefore, the program would not print out anything. But apperently, one is wrong. I have compiled this code with gcc (Ubuntu 9.3.0-17ubuntu1~20.04), g++ (Ubuntu 9.3.0-17ubuntu1~20.04), clang (10.0.0-4ubuntu1) and an online c compiler(https://www.onlinegdb.com/online_c_compiler), all with the same result: the printout "function returned true".
Further experimenting revealed that:
Replacing "FALSE" with "TRUE" (see code snippet below), build it, execute it, will result in the same printout ("function returned true"). Undoing the change and again build it and execute it doesn't change the output, no matter if the application file is deleted inbetween builds or not.
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int function_returning_false() {return TRUE;}
int main() {
if (function_returning_false) {
printf("function returned true\n");
}
}
Including the stdbool
-library and replacing the defines "FALSE" and "TRUE" with "false" and "true" doesn't make a difference (see code snippet below).
#include <stdio.h>
#include <stdbool.h>
#define TRUE 1
#define FALSE 0
int function_returning_false() {return false;}
int main() {
if (function_returning_false) {
printf("function returned true\n");
}
}
Replacing the function "function_returning_false()" in the "if" statement with the boolean value "false" (see code snippet below), build it, execute it, the application will have no printout, as expected. But if the change is undone immediately afterwards and the code is built and executed again, the application will henceforth work as it should. The described phenomena will reappear after the application is deleted and the machine on which it was built is rebooted.
#include <stdio.h>
#include <stdbool.h>
#define TRUE 1
#define FALSE 0
int function_returning_false() {return false;}
int main() {
if (false) {
printf("function returned true\n");
}
}
Thank you for your time.
Solution
You didn't copy the program correctly. This is the real program that reproduces the bug:
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int function_returning_false() {return FALSE;}
int main() {
if (function_returning_false) { // note, no ()
printf("function returned true\n");
}
}
So, it's not calling the function - it is passing the function as an argument to if
and in boolean context, a function will always be true
.
Answered By - Ted Lyngmo