- C Language, C++, Functions, loop, Program Output, Programming Language, Strings

What will happen if a print() statement is written inside a if() such as if(print())

Pre-requisite: if-elseThis article focuses on discussing what happens when the print statement is used inside the if-else conditional statement.For example: Consider the below code and predict the output.C  #include   int main(){        if (printf(“I’m Awesome!n”))                  main();          else if (printf(“I’m Not Awesomen”))                  main();}C++  #include using namespace std;  int main(){        if (printf(“I’m Awesome!n”))                  main();          else if (printf(“I’m Not Awesomen”))                  main();    return 0;}Guessing the Expected Output: Most general guess for the output of this program is some sort of ERROR.Actual Output: But its actual output is a bit strange and not the expected one. Explanation: Let’s discuss the reason.As if condition is being satisfied every time and due to recursive call of the main() function, this function call will ideally go on infinitely but in the real scenario, it’ll stop once the allowed function call stack will be filled.  So, first of all, let’s understand why the print function inside if condition is not giving error and how it’s working internally.  This behavior is based on a simple fact that print function like printf in C/C++ etc returns an integer value equal to the number of characters it has printed successfully.  Let’s understand this by a program below-C#include   int main(){        printf(“%d”, printf(“GFG!n”));    return 0;}C++#include using namespace std;  int main(){        printf(“%d”, printf(“GFG!n”));    return 0;}OutputGFG!
5Explanation: When the printf statement is encountered it goes to the outer printf function and tries to print its value and when the program tries to print its value then it encounters another printf and it goes to inner printf (like a recursive call) to execute it and then it executes it and that inner printf function returns 5 because that function has successfully printed 5 characters (‘G’, ‘F’, ‘G’, ‘!’, and ‘n’) and hence return an integer value equal to 5 in this case. Will print() used inside if always return true?From the above example, it can be easily understood how printf functions are working inside if statements and how the condition is being satisfied. The condition will always satisfy until and unless an empty string is printed like as shown in the below code- The only idea behind this example is to demonstrate if the printf function returns “0” only then the if condition would turn out to be false. In all other cases, it’ll be true whether the length of the string is 1 or 9999. C#include   int main(){        if (printf(“”))                  printf(“Condition is True”);          else                  printf(“Condition is False”);    return 0;}C++#include using namespace std;  int main(){        if (printf(“”))                  printf(“Condition is True”);          else                  printf(“Condition is False”);    return 0;}