int i=45;
i++;
printf(" Output : %d \n", i );
i=20;
i--;
printf(" Output : %d \n", i );
Output
Output : 46
Output : 19
Increase by more than 1
int i=45,j=0;
i+=5;
printf(" Output : %d \n", i ); // 50
When we use assignment operator with increment and decrement operator then assignment is executed first and then increment ( or decrement ) is executed.
int i=45,j=0;
// first value is assigned to j, then increment is done to i
j=i++;
printf(" Output : %d \n", j ); // 45
In above code output is 45 ( NOT 46 ), but if you change to display value of i ( in place of j inside printf() ) , we will get 46.
Logical operators
Operator
Detils
&&
And operator. Output true only if all operands are true
||
Or operator. Output true if any one of the operand is true
!
Not operator. True if out put of condition is false
Examples ( Output is after comment )
#include <stdio.h>
int main()
{
int x=40 ,y=50;
int result;
result= (x==40) && (y ==30);
printf(" Output : %d \n", result ); // 0
result= (x==40) || (y ==30);
printf(" Output : %d \n", result ); // 1
result= !(x==40) || (y ==30);
printf(" Output : %d \n", result ); // 0
}
Relational operators
Here output is always True or False. It can be 1 or 0
Operator
Detils
==
Equal To
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
!=
Not equal to
Example of Relational Operators ( output are shown with comments )
#include <stdio.h>
int main()
{
int x=30 ,y=20;
printf(" Output : %d \n", x == y); // 0
printf(" Output : %d \n", x < y); // 0
printf(" Output : %d \n", x > y); // 1
printf(" Output : %d \n", x <= y); // 0
printf(" Output : %d \n", x >= y); // 1
printf(" Output : %d \n", x != y); // 1
}
Practice Questions on logical and relational operators
Write the expected output of these lines of code
int x=50,y=40;
printf(" Output : %d \n", x == y);
printf(" Output : %d \n", x < y);
printf(" Output : %d \n", x > y);
printf(" Output : %d \n", x <= y);
printf(" Output : %d \n", x >= y);
printf(" Output : %d \n", x != y);
Understanding && and || operators ( AND OR ), write the expected outputs
int x=45,y=25;
int result;
result= x==45;
printf(" Output : %d \n", result );
result= x!=45;
printf(" Output : %d \n", result );