Operators In C Progreamming



                      Operators In C


An operator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition.
C programming has wide range of operators to perform various operations. For better understanding of
operators, these operators can be classified as:

Types Of Operators

1.Arithmetic Operators

2.Increment and Decrement Operators

3. Assignment Operators

4.Relational Operators

5.Logical Operators

6.Bitwise Operators

7.Conditional Operators

8.Special Operators


1.Arithmetic Operators :

An arithmetic operator performs mathematical operations such as addition,
subtraction and multiplication on numerical values (constants and variables).

Example:

#include <stdio.h>
#include <conio.h>
int main()
{
    int a = 9,b = 4, c;
    
    c = a+b;
    printf("a+b = %d \n",c);

    c = a-b;
    printf("a-b = %d \n",c);
    
    c = a*b;
    printf("a*b = %d \n",c);
    
    c=a/b;
    printf("a/b = %d \n",c);
    
    c=a%b;
    printf("Remainder when a divided by b = %d \n",c);
    
    return 0;
}

2.Increment and Decrement Operators :

C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators,
meaning they only operate on a single operand.

Example:

#include <stdio.h>
#include <conio.h>
int main()
{
    int a = 10, b = 100;
    float c = 10.5, d = 100.5;

    printf("++a = %d \n", ++a);

    printf("--b = %d \n", --b);

    printf("++c = %f \n", ++c);

    printf("--d = %f \n", --d);

    return 0;
}

3. Assignment Operators :

An assignment operator is used for assigning a value to a variable. The most common assignment operator is =

Example:

#include <stdio.h>
#include <conio.h>
int main()
{
    int a = 5, c;

    c = a;
    printf("c = %d \n", c);

    c += a; // c = c+a
    printf("c = %d \n", c);

    c -= a; // c = c-a
    printf("c = %d \n", c);

    c *= a; // c = c*a
    printf("c = %d \n", c);

    c /= a; // c = c/a
    printf("c = %d \n", c);

    c %= a; // c = c%a
    printf("c = %d \n", c);

    return 0;
}

4.Relational Operators :

A relational operator checks the relationship between two operands.
If the relation is true, it returns 1; if the relation is false, it returns value 0.

Example:

#include <stdio.h>
#include <conio.h>
int main()
{
    int a = 5, b = 5, c = 10;

    printf("%d == %d = %d \n", a, b, a == b); // true
    printf("%d == %d = %d \n", a, c, a == c); // false

    printf("%d > %d = %d \n", a, b, a > b); //false
    printf("%d > %d = %d \n", a, c, a > c); //false


    printf("%d < %d = %d \n", a, b, a < b); //false
    printf("%d < %d = %d \n", a, c, a < c); //true


    printf("%d != %d = %d \n", a, b, a != b); //false
    printf("%d != %d = %d \n", a, c, a != c); //true


    printf("%d >= %d = %d \n", a, b, a >= b); //true
    printf("%d >= %d = %d \n", a, c, a >= c); //false


    printf("%d <= %d = %d \n", a, b, a <= b); //true
    printf("%d <= %d = %d \n", a, c, a <= c); //true

    return 0;

}

5.Logical Operators :

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.
Logical operators are commonly used in decision making in C

Example:

#include <stdio.h>
#include <conio.h>
int main()
{
    int a = 5, b = 5, c = 10, result;

    result = (a == b) && (c > b);
    printf("(a == b) && (c > b) equals to %d \n", result);

    result = (a == b) && (c < b);
    printf("(a == b) && (c < b) equals to %d \n", result);

    result = (a == b) || (c < b);
    printf("(a == b) || (c < b) equals to %d \n", result);

    result = (a != b) || (c < b);
    printf("(a != b) || (c < b) equals to %d \n", result);

    result = !(a != b);
    printf("!(a == b) equals to %d \n", result);

    result = !(a == b);
    printf("!(a == b) equals to %d \n", result);

    return 0;
}

6.Bitwise Operators :

During computation, mathematical operations like: addition, subtraction,
addition and division are converted to bit-level which makes processing faster and saves power.

Example:

#include <stdio.h>
#include <conio.h>
int main()
{
    int a, e[10];
    float b;
    double c;
    char d;
    printf("Size of int=%lu bytes\n",sizeof(a));
    printf("Size of float=%lu bytes\n",sizeof(b));
    printf("Size of double=%lu bytes\n",sizeof(c));
    printf("Size of char=%lu byte\n",sizeof(d));
    printf("Size of integer type array having 10 elements = %lu bytes\n", sizeof(e));
    return 0;
}




                                  <<   PREVIOUS  >>


                                 <<      NEXT         >>

Comments