Ternary operators in C, wrong output
Solution 1:
The printf()
function returns the number of characters printed to stdout
. Therefore, the value 19 is assigned to the variable ll. The expression "\n kk is equal to 65"
is printed on the screen, followed by the value of the variable ll
.
#include <stdio.h>
int main( )
{
int kk = 65, ll;
ll = (kk == 65) ? printf( "\n kk is equal to 65" ) : printf ( "\n kk is not equal to 65" );
printf("\nll: %d", ll);
printf("\nNumber of character: %d", printf( "\n kk is equal to 65" ));
return 0;
}
This program generates the following output:
kk is equal to 65
ll: 19
kk is equal to 65
Number of character: 19