In the first program below there is no error.But for the second program there is an error.
Why is that so?
First program:
#include<stdio.h>
void main()
{
int k=8;
int m=7;
k<m?k=k+1:m+1;
printf("%d",k);
}
Second program:
#include<stdio.h>
void main()
{
int k=8;
int m=7;
k<m?k=k+1:m=m+1;
printf("%d",k);
}
The conditional operator has higher precedence than the assignment operator. You need extra parentheses to have the desired precedence.
is evaluated as
Add parentheses to have the correct precedence: