Sunday, 10 September 2017

Explain the working of shorthand assignment operators, pre and post increment operator and the ternary operator.

Q : Explain the working of shorthand assignment operators, pre and post increment operator and the ternary operator.

Solution : 

Assignment Operator

Prefix operator

Add or subtract a variable value by one. There are two types of prefix operator increment and decrement. If use in any expression or equation then first increment or decrement variable then solve related equitation with new value of related variable.

int a,b=10;

a=++b+5;
printf("A=%d\nB=%d",a,b);

Output:
A=16,B=11

Postfix Operator

Add or subtract a variable value by one. There are two types of prefix operator increment and decrement. If used in any expression or equitation then first solve equitation with current value of variable. After solve equitation change variable value by one depend on operator.

int a,b=10;
a=b++ +5;
printf("A=%d\nB=%d",a,b);

Output:
A=15,B=11

Ternary Operator

Assignment with condition. When there is a need to assign value to a variable with condition.

Syntax :

Variable=condition ? True Part : False Part


 int n,m=10;
n=m>10?1:0;

n will initialize with 0 because m is not greater than 10