C Operators and Expressions

Operators and Expressions


An Operator specifies an operation to be performed that accepts operands, on which operation is done and output is produced to form an Expression. An Expression is a statement in which variables or constants are joined by various operators. An operand is a data item on which an operator acts. C language includes the following category of operators :

1. Arithmetic Operators 

Arithmetic operators are used for numeric calculations. There are two types of Arithmetic Operators :
(i) Unary - These operates only on one operand. For example -  +x ,  -y etc.
(ii) Binary - binary operator require two operands. There are five binary arithmetic operators:

Operator
Operation
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Gives the remainder in integer division

Program Example :
#include<stdio.h>
// main function starts here
int main() {
    int x=40;
    int y=25;

    int result=x+y;
    printf("Sum of x+y = %d",result);

    result=x-y;
    printf("\nDifference of x-y = %d",result);

    result=x*y;
    printf("\nProduct of x*y = %d",result);

    result=x%y;
    printf("\nRemainder of x%y = %d",result);

    float a=40, b=25, solution=a/b;
    printf("\nDivision of a/b = %f",solution);
}

Click on Run

2. Assignment Operator 

Assignment Operator is used to assign or provide value to a Variable. Giving or providing values is known as Assignment. This assignment operator "=" is used in assignment expressions.
In assignment, The value of right hand operand is assigned to the left hand operand. Some of the examples are :-
int x = 15;     
/* value 15 is assigned to variable x */
int y = 5;     
/* value 5 is assigned to variable y */
x = x+y;     
/* value of x+y(20) is assigned to variable x */
y = x+5;     
/* value of x+5 (25) is assigned to variable y */
x = y;     
/* value of variable 'y' is assigned to variable x */

Shorthand Rules in Assignment :

when the same variable is used in both sides of "=" , then we use the Shorthand notation for saving our time.
For Example -
x = x +15;

can also be written as -
x+ = 15;
Here += is a Compound assignment Operator.

Expression
Shorthand Notation
x = x-10;
x -= 10;
x = x*10;
x *= 10;
x = x/5;
x /= 5;
x = x%4;
x %= 4;
x = x+1;
x += 1;

3. Increment and Decrement Operator 

Numerous times while writing a C program we need to to add 1 to our variable. C provides 2 useful operators:-  
(i) increment (++) and 
(ii) decrement (--) Operators. 
These are unary operators because they operate on a single Operand. The increment operator (++) increments the value of variable by 1 and decrement operator (--) decrements the value of the variable by 1.
int x = 5;
/* 5 is assigned to variable x */

++x ;        
/* This is equivalent to : 
x = x+1;  (x now holds value 6)
*/

--x ;
/* This is equivalent to : x = x-1; */
Note :- These operators should be used only with variables; they cannot be used with constants or expression For example the expressions ++5 or ++(x+y+z) are invalid.

These operators are of two types :-

(a.) Prefix Increment/Decrement - Here, first the value of Variable is incremented / decremented then the new value is used in the operation. Let's understand with the help of an example:
int x = 5;
// 5 is assigned to x
int y = ++x ;
/* First 'x' is incremented by 1, then assigned to y */

printf("y : %d",y);
/* Outputs : "y : 6" */

y = --x ;
// y holds value 5 again    

printf("y : %d",y);
/* Outputs : "y : 5" */

(b.) Postfix Increment / Decrement - Here, first the value of variable is used, and then increment/decrement is performed. Let's understand this with the help of an example :
int x = 5;
// 5 is assigned to x
int y = x++ ;
/* First, 'x' is assigned to 'y', then increment occurs, thus 'y' holds 5 */
/* 'x' will be incremented by 1 in the next expression */

int z = x;
/* z is assigned the value of x, i.e, 6 */

printf ("y : %d \nz : %d",y,z);

/* 
OUTPUT :
y : 5
z : 6
 */

4. Relational Operator 

Relational operators are used to compare values of two expressions depending on their relations. An expression that contains relational operators is called relational expression. "If a relation is true then the value of relation expression is 1 and if the relation is false then the value of expression is 0". The relational operators are given under :

Operator
Meaning
<
Less than
<=
Less than or equal to
==
Equals to
!=
Not equals to
>
Greater than
>=
Greater than or equal to

Lets take 2 variables x and y with value 5 and 7 respectively.
int x = 5, y = 7;
and lets perform relational operations on them :

Expression
Result
Value of Expression
x < y
True
1
x <= y
True
1
x == y
False
0
x!= y
True
1
x > y
False
0
x >= y
False
0
x == 5
True
1
y != 7
False
0
x > 7
False
0
y > 5
True
1

Program example : 
#include<stdio.h>
//main function starts here
int main() {
    int x=10;
    int y=25;
    
    if (x!=y)
    printf("x is not equals to y\n");
    if (x==y)
    printf("x is equals to y\n");
    if (x<y)
    printf("x is smaller than y\n");
    if (x>y)
    printf("x is larger than y\n");
    if (x==10)
    printf("x is equal to 10\n");
    if (y>15)
    printf("y is greater than 15\n");
}
Code Explanation  
In this code, we have used if() decision making structure. Let's understand the working of if() structure. It has the following syntax :
if (condition)
statement ;
if() structure is used to test the condition written inside the brackets or parentheses '()'. In C, if any condition returns true i.e 1 in Binary; then, the structure will execute otherwise not.

We have first declared 2 variables of int type and assign them values
int x=10;
int y=25;
Then, We will test for our conditions using relational Operator and if() structure.
if (x!=y)
/* This will return true or Binary 1 */
    printf("x is not equals to y\n");
First, Checking whether "x is not equals to y", which is true, thus condition is replaced by Binary 1 during the execution of the program and the statement after it runs.
if (x==y)
/* This will return false or Binary 0 */
    printf("x is equals to y\n");
This will return false or Binary 0 as "x is equals to y" is not true. So, the statement after it will not execute.
if (x<y)
/* This will return true or Binary 1 */
    printf("x is smaller than y\n");

if (x>y)
/* This will return false or Binary 0 */
    printf("x is larger than y\n");
The first statement will execute and the second will not.
if (x==10)   
/* This will return true or Binary 1 */
    printf("x is equals to 10\n");

if (y>15)     
/* This will return true or Binary 1 */
    printf("y is greater than 15\n");
Both the statements will return true thus both statements will be executed.

Post a Comment