-1

Consider the following C code, according to what I have learnt the OR statement should evaluate both the printf. But in actual output I see only "XX". Why is this happening?

#include<stdio.h>
int main() {
    int a;
    a = (printf("XX")||printf("YY"));
    printf("%d\n",a);
    a = (printf("XX")&&printf("YY"));
    printf("%d\n",a);
}

Output -

XX1 XXYY1

kame
  • 20,848
  • 33
  • 104
  • 159
Agile_Eagle
  • 1,710
  • 3
  • 13
  • 32

4 Answers4

2

OR operator output is true even if one condition is true. And in this case first printf statement returns true. So there is no need to evaluate the second operand of OR operator.

Nithin Kumar Biliya
  • 2,763
  • 3
  • 34
  • 54
2

|| operator evaluates to true if any of the operands is true. So if first operand evaluates to true it doesn't check the second. It only checks second operand if first operand was false.

&& operator evaluates to true only when both the operands are true. It would check the second operand iff the first one is not false.

As stated in the manpage:

Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings). The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. (See also below under NOTES.)

Say you have a code like:

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("%d", printf(""));
    return 0;
}

This would print 0 just as explained in the manpage.

If your statement is:

printf("") && printf("XX");

It won't print anything because first operand evaluated to 0.

Whereas,

printf("") || printf("YY");

Would print YY.

In your case,

a = (printf("XX")||printf("YY"));

would evaluate printf("YY") only when the first operand failed to print anything.

a = (printf("XX")&&printf("YY"));

Would print XXYY when both printf were successful.

Mihir Luthra
  • 6,059
  • 3
  • 14
  • 39
1

Say you're trying to answer this question "Is it raining or is it after 7 PM?". Once you see that it's raining, you know the answer is "yes". You have no need to check if it's after 7 PM or not.

a = (printf("XX")||printf("YY"));

You asked it an "OR" question. Once it evaluates the first printf, it knows that the answer to your question is yes, so it has no need to evaluate the second part.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

The || or OR operator doesn't evaluate its right part if the left part has been determined to be true. Because it does not need to: TRUE || something is "always" true. Even if a potential crash hides on the right. The left part is always checked first.

The return value of printf is not "false" in case of error: it return -1. The "false" value is 0, which would occur if you are trying to print an empty format string, or put a '\0' at the beginning of it. Any value that is not 0 is evaluated as "true" in the general case.

AugustinLopez
  • 348
  • 1
  • 3
  • 13