Not understanding the 'OR" operator

I have this simple little program code and I did not get the results I expected.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
    int height = 5;
    int width = 10;

    if (width == printf("123") ||height == 5)
    {
        cout << endl <<("'If' statement evaluated as true") <<endl;
    }
    if (height == 4 || width == printf("123"))
        cout << ("Something is not understood!");
    return 0;
}


The output of the first "if" statement is what I expected.
The output of the second "if" statement is not what I expected. The first parameter,"height ==4" evaluates to false, but the second parameter, "width == printf("123") evaluates to true, so why doesn't the cout command print out the sentence, "Something is not understood!"
No it does exactly what you tell it to do. Height is not 4. and Width is equal to 10.
What are you expecting the return value of printf("123") to be?
Because it's not going to be 10. printf returns the number of characters it prints.
Last edited on
Thank you for the responses.
I understand that the output is correct. It is me that is the problem. I don't understand why the last 'cout' is not done. I am seeing the second parameter "width == printf("123") as being true, because as you pointed out, 'width' does indeed, equal 10. . If it is true, I am expecting the block code to be executed.
I need help straightening out my brain process!
I am seeing the second parameter "width == printf("123") as being true, because as you pointed out, 'width' does indeed, equal 10.

width is 10 but printf("123") does not return 10. It returns 3. 10 is not equal to 3 so the comparison is false.
Of course! Thanks so much for seeing me through this "blind" spot. I really do appreciate it.
Topic archived. No new replies allowed.