× Back Arithmetic operator Assignment operator Increment and Decrement Operators Relational Operators Logical Operators Conditional Operator Bitwise Operator sizeof Operator Comma Operator Control Statements Programs
Next Topic → ← Previous Topic

Operators and Expressions

Operators are the symbols which performs specific task on the operands.

There are three types of operators on the basis of operands

  1. Unary operator → operators which performs operation on single operand.
  2. Binary operator → operators which performs operation on two operands.
  3. Ternary operator → operators which performs operation on three operands.

Operators on the basis of operations

  1. Arithmetic operator
  2. Assignment operator
  3. Relational operator
  4. Logical operator
  5. Bitwise operator
  6. Conditional operator
  7. Increment and decrement operator
  8. sizeof operator
  9. comma operator

Arithmetic operator

Arithmetic operators are used for numberic calculations. They are of two types-

  1. Unary arithmetic operator
    • requires only one operand.
    • Example → -y
    • here '-' changes the sign of the operand y.
  2. Binary arithmetic operator
    • require two operands. There are five binary arithmetic operators.
      • + → addition
      • - → subtraction
      • * → multiplication
      • / → division
      • % → remainder
    • ☆ note → (%) modulus operator cannot be applied with floating point operands.

Assignment Operator '='

  • A value can be stored in a variable using assignment operator.
  • The operand on the ldft hand side should be a variable, while the operand on the right hand side xan be any variable, constant or expression.
  • Examples:
    a = 6;
    a = x + y - 6;

Increment and Decrement Operators

  • There are two operators increment ( ++ ) and decrement ( -- ).
  • These are unary operators as they operate on single operand.
  • ( ++ ) increments the value of the variable by 1 and ( -- ) decrements the value of the variable by 1.
  • ++x → x = x + 1
    --x → x = x - 1
  • These operators are of two types
    1. Prefix increment / decrement → ( ++x or --x )
    2. Postfix increment / decrement → ( x++ or x-- )

Prefix Increment / Decrement

  • The value of variable is first incremented / decrement then the new value is used in the operation
                            
#include <stdio.h>

int main()
{
    int x = 5, y, b = 10, c;
    y = ++x;
    c = --b;
    printf("The value of y is = %d and x is = %d\n", y, x); // The value of y is = 6 and x is = 6
    printf("The value of c is = %d and c is = %d\n", c, b); // The value of c is = 9 and c is = 9
    return 0;
}
                            
                        

Postfix Increment / Decrement

  • The value of variable is used in the operation and then increment / decrement is performed.
                            
#include <stdio.h>

int main()
{
    int x = 5, y, b = 10, c;
    y = x++;
    c = b--;
    printf("The value of y is = %d and x is = %d\n", y, x); // The value of y is = 5 and x is = 6
    printf("The value of c is = %d and c is = %d\n", c, b); // The value of c is = 10 and c is = 9
    return 0;
}
                            
                        

Relational Operators

  • These are used to compare values of two expressions.
  • If the relation is true then value of relation expression is 1 and if the relation is false then the value of expression is 0.
  • The relational operators are-
    • < ( less than )
    • ≤ ( less than or equal to )
    • = = ( equal to )
    • != ( Not equal to )
    • > ( Greater than )
    • ≥ ( Greater than or equal to )

Logical or Boolean Operators

  • These combines two or more expressions
  • For combining these expressions we use logical operators
  • These operators return 0 or 1.
  • Three logical operators
    1. && ( AND )
    2. || ( OR )
    3. ! ( NOT )

Conditional Operator

  • Also known as ternary operator ( ? and : ) which requires three expressions as operands.
    TestExpression ? expression1 : expression2
    • If testExpression is true, then expression1 is evaluated.
    • If textExpression is false, then expression2 is evaluated.
                        
a < b ? printf("a is smaller") : printf("b is smaller");
                        
                    

Bitwise Operators

  • These are used for operations on individual bits.
  • These operate on integers only.
  • Bitwise operators are
    • & ( bitwise AND )
    • | ( bitwise OR )
    • ~ ( one's complement )
    • << ( left shift )
    • >> ( right shift )
    • ^ ( bitwise XOR )

sizeof Operator

  • gives the size of a any data type
                        
sizeof(int); // 4
sizeof(float); // 4
                        
                    

Comma operator

  • used to separate variable, expressions.
                        
int a,b,c;
                        
                    

Precedence and Associativity of Operators

Operator

Description

Precedence Level

Associativity

( )


[ ]

.

Function call


Array subscript
Arrow operator
Dot operator

1

left to right

+


-
++
--
!
~
*
&
sizeof

Unary plus


Unary minus
increment
decrement
logical NOT
one's complement
indirection
address
sizeof

2

left to right

*


/
%

multiplicaiton


division
modulus

3

left to right

+


-

addition


subtraction

4

left to right

<<


>>

left shift


right shift

5

left to right

<


<=
>
>=

less than


less than or equal to
greater than
greater than or equal to

6

left to right

= =


!=

equal to


not equal to

7

left to right

&

Bitwise AND

8

left to right

^

Bitwise XOR

9

left to right

|

Bitwise OR

10

left to right

&&

logical AND

11

left to right

||

logical OR

12

left to right

? :

conditional operator

13

right to left

=


*=   /=   %=
+=   -=
&=   ^=   |=
<<=   >>=

assignment operator

14

right to left

,

comma operator

15

left to right

Questions

  • a = 8, b = 4, c = 2, d = 1, e = 5 , f = 20
    a+b-(c+d)*3%e+f/9
  • a =17, b = 5, c = 6, d = 3, e = 5
    a % 6 - b / 2 + ( c * d - 5 ) / e
  • a = 4, b = 5, C = 6, d = 3, e = 5, f = 10
    a*b-c/d<e+f
  • a = 8, b = 5, c = 8, d = 3, e = 65, f = 10, g = 2, h = 5, k = 2
    a-b+c/d== e/f - g+h %k
  • a = 8, b = 3, C = 2, d = 3, e = 2, f = 11
    a - b || ( a- b * C ) + d && e - f % 3

Control Statements

If...else

Syntax ↓

                    
if(condition)
    statement1;
                    
                
                    
if(condition)
{
    statement;
    ......
    .... 
}
                    
                

syntax 2↓

                    
if(condition)
    statement1;
else 
    statement2;
// Here if the condition is true then statement1 is executed and if it is false then statement2 is executed.
                    
                
                    
if(condition)
{
    statement;
    ......
    .... 
}
else 
{
    statement;
    ......
    ....  
}
                    
                

Nesting of if...else

  • Having if...else statement in the if block or the else block. This is called nesting of if...else statements.

Syntax ↓

                        
if(condition 1)
{
    if(condition 2)
        statementA1;
    else 
        statementA2;
}
else 
{
    if(condition 3)
        statementB1;
    else 
        statementB2;
}
                        
                    

else if Ladder

  • This is a type of nesting in which there is an if...else statement in every else part except the last else part. This type of nesting is frequently used in programs and is also known as else if ladder.
                        
if(condition1)
    statementA;
else 
    if(condition2)
        statementB;
    else 
        if(condition3)
            statementC;
        else 
            statementD;
                        
                    

Loops

while loop

                        
while(conditon)
{
    statement;
    statement;
}
                        
                    

Program to print the numbers from 1 to 10 using while loop.

                            
#include<stdio.h> 
int main()
{
    int i = 1;
    while(i<=10)
    {
        printf("%d\t",i);
        i++;
    }
    printf("\n");
}
                            
                        

Output ↓

                            
1   2   3   4   5   6   7   8   9   10
                            
                        

do...while loop

  • The 'do...while' statement is also used for looping.

Syntax ↓

                    
do 
{
    statement;
    statement;
    .......
}while(condition);
                    
                

Program to print the numbers from 1 to 10 using do...while loop

                            
#include<stdio.h> 
int main()
{
    int i = 1;
    do
    {
        printf("%d\t",i);
        i++;
    }while(i<=10);
    printf("\n");
}
                            
                        

Output↓

                            
1   2   3   4   5   6   7   8   9   10
                            
                        

for loop

Syntax ↓

                        
for(expression1;expression2;expression3)
{
    statement;
    statement;
}
                        
                    

Nesting of Loops

Program to understand nesting of for loop

                            
#include<stdio.h>
int main()
{
    int i,j;
    for(i=1;i<=3;i++) // outer loop
    {
        printf("i = %d\n",i);
        for(j=1;j<=4;j++)
            printf("j = %d\t",j);
        printf("\n");
    }
    return 0;
}
                            
                        

Break Statement

Continue Statement

  • The difference between break and continue is that when break is encountered the loop terminates and the control statement is transferred to the next statement following the loop, but when a continue statement is encountered the loop is not terminated and the control is transferred to the beginning of the loop.

Goto

                    
goto label;
    .....
    .....
label:
    statement;
    ......
    ......
                    
                

Program to print whether the number is even or odd.

                        
#include<stdio.h>
int main()
{
    int n;
    printf("Enter the number : ");
    scanf("%d",&n);
    if(n%2==0)
        goto even;
    else 
        goto odd;
    even :
        printf("Number is even");
        goto end;
    odd :
        printf("Number is odd");
        goto end;
    end :
        printf("\n");
        
    return 0;
}
                        
                    

Switch

  • It is used to perform different actions based on different conditions (cases).
  • Switch case statements follow a selection-control mechanism and allow a value to change control of execution.
  • This is a multi-directional conditional control statement.
  • Sometimes there is a need in program to make choice among number of alternatives, for making this choice, we use switch statement.
  • This can be written as -
                        
switch(expression)
{
    case constant1:
        statement 
        ......
        break;
    case constant2:
        statement 
        ......
        break;
    case constant3:
        statement 
        ......
        break;
    default:
        statement 
        ......
}
                        
                    

Switch vs if else

  • Switch is better for multi way branching.
  • Switch is faster than if...else if provided with good number of cases.
  • Switch looks much cleaner when you have to read it.

Programs

Program to find factorial using for loop.

                        
#include <stdio.h>

int main()
{
    int n,fact=1;
    printf("Enter the number whose factorial you want to calculate : ");
    scanf("%d",&n);
    for (int i = 1; i <= n; i++)
    {
    fact*=i;
    }
    printf("The factorial of %d is %d\n",n,fact);
    return 0;
}
                        
                    

Program to find factorial using while loop.

                        
#include <stdio.h>
int main()
{
    int n,fact=1;
    printf("Enter the number whose factorial you want to calculate : ");
    scanf("%d",&n);
    int i = 1;
    while(i<=n)
    {
    fact*=i;
    i++;
    }
    printf("The factorial of %d is %d\n",n,fact);
    return 0;
}
                        
                    

Program for using +,-,/,*,% operator in calculator

                        
#include <stdio.h>
int main()
{
    int a, b;
    char op;
    printf("Enter the value of 'a' and 'b' : ");
    scanf("%d%d", &a, &b);
    printf("Enter the operation from : '+', '-', '/', '*', '%%' : ");
    scanf(" %c", &op);
    if (op == '+')
        printf("The sum of %d and %d is %d\n", a, b, a + b);
    else if (op == '-')
        printf("The subtraction of %d and %d is %d\n", a, b, a - b);
    else if (op == '*')
        printf("The multiplication of %d and %d is %d\n", a, b, a * b);
    else if (op == '/')
        printf("The division of %d and %d is %d\n", a, b, a / b);
    else if (op == '%')
        printf("The modulus of %d and %d is %d\n", a, b, a % b);
    else
        printf("You enter wrong operator\n");

    return 0;
}
                        
                    
                            
#include <stdio.h>

int main()
{
    char op;
    int a, b, c;
    printf("Enter the value of a and b : ");
    scanf("%d%d", &a, &b);
    printf("Enter the operator : ");
    scanf(" %c",&op);
    switch (op)
    {
    case '+':
        c = a + b;
        printf("sum = %d\n", c);
        break;
    case '-':
        c = a - b;
        printf("subraction = %d\n", c);
        break;
    case '*':
        c = a * b;
        printf("multiplication = %d\n", c);
        break;
    case '/':
        c = a / b;
        printf("division = %d\n", c);
        break;
    case '%':
        c = a % b;
        printf("remainder = %d\n", c);
        break;

    default:
        printf("Entered the wrong operator\n");
        break;
    }
    return 0;
}
                            
                        

Program to find whether the alphabet is a vowel or consonant

                            
#include <stdio.h>

int main()
{
    char ch;
    printf("Enter alphabet : ");
    scanf("%c", &ch);
    switch (ch)
    {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        printf("The entered alphabet is a vowel\n");
        break;
    default:
        printf("The entered alphabet is a consotant\n");
        break;
    }
    return 0;
}
                            
                        

Program to print
*
* *
* * *
* * * *
* * * * *

                                                   
#include <stdio.h>

int main()
{
    for (int i = 1; i <= 5; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
                            
                        

Program to print
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

                            
#include 

int main()
{
    for (int i = 1; i <= 5; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}
                            
                        

Program to print
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

                            
#include <stdio.h>

int main()
{
    for (int i = 1; i <= 5; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            printf("%d ",i);
        }
        printf("\n");
    }
    return 0;
}
                            
                        

Program to print

* * * * *
* * * *
* * *
* *
*

                            
#include <stdio.h>
int main()
{
    for(int i = 5;i>=1;i--)
    {
        for(int j=i;j>=1;j--)
        {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
                            
                        

Program to print
                        
        *
      * *
    * * *
  * * * *
* * * * *
                        
                    

                            
#include <stdio.h>
int main()
{
    for(int i = 5;i>=1;i--)
    {
        for(int j=i;j>1;j--)
        {
            printf("_");
        }
        for(int k = 1;k<=5-i+1;k++)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}
                            
                        

Write a program to enter a number and find the reverse of that number.

                            
#include <stdio.h>

int main() {
    
    int num = 3354, rev = 0;
    while (num > 0)
    {
        rev *= 10;
        rev += (num % 10);
        num /= 10;
    }
    printf("The reverse is : %d", rev);
    
    return 0;
}