int a = 0;
a = 7 > 2 ? printf("6") : printf("4");
printf ("%d",a);
The ouput of this block is :
61
I tried the code in its expanded form
int a = 0 ;
if(7>2)
{
printf("6");
}
else
{
printf("4");
}
printf("%d",a);
Here I the output was:
60
I would like to get an explanation on why the output differs.
CodePudding user response:
The first statement assigns the return value of printf
to a
. printf returns the number of bytes that were written. In this case that is 1. In the second expanded version, a
is not assigned. Here is an actually equivalent version to the original:
int a = 0;
if(7>2)
{
a = printf("6");
}
else
{
a = printf("4");
}
printf("%d",a);
CodePudding user response:
They are completely different.
To make them identical:
int a = 0 ;
if(7>2)
{
a = printf("6");
}
else
{
a = printf("4");
}
printf("%d",a);
CodePudding user response:
cond?val1:val2
is the ternary operator. It is not supposed to be a control structure (like if
, for
or while
). It is an operator. To build expression (things that have a value) rather than instruction (things that does things).
Frontier is fuzzier in C than in other languages, because instructions have a value (including void
) and expression have potential side-effects.
But, well, you use cond?val1:val2
when you want to get the result. As you did, since you assigned the result to a
.
And the result here is the result of printf("6")
, that is 1, since printf
returns the number of printed characters. Note that there is no real doubt, since even if 7 were smaller than 2, result would still have been 1. Since you print that result, it is normal to have a 1
printed after the 6
.
(Just to be clear, even if I assume you know that already, what you did is print string "6" and then number 1, which is the result of 1st printf. Exactly as if you did
printf("%d",printf("6"));
which 1st prints "6", then pass the result to the outer printf to print what the inner printf
returned, that is 1)
In your second code, you do nothing to change a
's value, and you ignore the result of printf
.
CodePudding user response:
Your examples aren't equivalent. To be equivalent, the second one should say a=printf(...
everywhere. After which a
will get assigned the number of characters printed, 1
.
CodePudding user response:
The conditional operator (e1 ? e2 : e3
) is not a "short version of if condition", although it has some similarities with an if ... else
construct.
As others have said, your first example assigns a value to a
, because it is written in the form of an assignment statement; that value is the 1
returned by the call to the printf
function. To get similar behaviour in your second example, as others have said, you need to also assign the value returned by printf
inside the if ... else
blocks.
Alternatively, to make the first case work like the second, you can skip the assignment and use the conditional operation to determine the argument that is passed to the printf
call:
int main(void)
{
int a = 0;
printf(7 > 2 ? "6" : "4");
printf("%d", a);
return 0;
}