C Programming

3-Operators in C Programming Language

C Language, C Programming, Code Blocks, Starting with C , Operators in C

In our previous article, we mentioned about arithmetic operators in C language.

In this article, we will cover “increment and decrement operators”, “assignment operators”, “relational operators”, ”logical operators” and “bitwise operators”.

Increment and Decrement Operators

Operators used for increment and decrement operations in C language are ” ++ ” and ” – – ” respectively. The result is increased or decreased by 1 according to the operator used.

//increment and decrement operators example
#include <stdio.h>

int main()
{
   int x = 5, y = 20;
   float a = 10.5, b = 100.5;

   printf(" ++x = %d \n", ++x); // 1 added to x
   printf(" --y = %d \n", --y); // 1 subtracted from y
   printf(" ++a = %f \n", ++a); // 1 added to a
   printf(" --b = %f \n", --b); // 1 subtracted from b

   return 0;
}
increment and decrement in C

C Programming Language. Operators in C. Code Blocks. Starting to C. C Programming Lessons. Increment Decrement Assignment Relational Bitwise.
Increment and Decrement in C

The order of operation results changes according to the location of the operators. The following example shows the difference using prefix and postfix.

#include <stdio.h>

int main()
{
   int x = 5, y = 10;

   // 5 is displayed, then, x is increased to 6
   printf(" %d \n", x++);

   // y is increased to 11, then it is displayed

   printf(" %d \n", ++y);

   // display increased x
   printf(" %d \n", x);

   return 0;
}
Prefix and Postfix in C
Prefix and Postfix in C

Assignment Operators

Operator         Example          Equal Expression

=                      x = y                x = y

+=                    x += y              x = x+y

-=                    x -= y               x = x-y

*=                    x *= y              x = x*y

/=                    x /= y               x = x/y

%=                   x %= y             x=x%y

/*Assignment Operators*/

#include <stdio.h>

int main()
{
    int x = 15, y;

    y = x;  // y is 15
    printf("y = %d\n", y);

    y += x; // y is 30
    printf("y = %d\n", y);

    y -= x; // y is 15
    printf("y = %d\n", y);

    y *= x; // y is 225
    printf("y = %d\n", y);

    y /= x; // y is 15
    printf("y = %d\n", y);

    y %= x; // y = 0
    printf("y = %d\n", y);

    return 0;
}
Assignment Operators in C

C Programming Language. Operators in C. Code Blocks. Starting to C. C Programming Lessons. Increment Decrement Assignment Relational Bitwise.
Assignment Operators in C

Relational Operators

Operator         Equivalent in C           Example          Equal Expression

=                      ==                               x==y                x equals to y

≠                      !=                                x!=y                 x is not equal to y

>                      >                                 x>y                  x greater than y

<                      <                                 x<y                  x is less than y

≥                      >=                               x=>y                x is greater than or equal to y

≤                      <=                               x<=y                x is less than or equal to y

If the condition is true, the statement in the “if” structure is executed, otherwise it is not executed.

// example__relational operators

#include <stdio.h>

int main()
{
    int x = 10, y = 10, t = 15; //defined variables

    //system will print 1 (true) if the condition is met, otherwise it will print 0 (false)

    printf("%d == %d is %d \n", x, y, x == y);

    printf("%d > %d is %d \n", x, y, x > y);
    printf("%d < %d is %d \n", x, t, x < t);

    printf("%d != %d is %d \n", x, y, x != y);

    printf("%d >= %d is %d \n", x, y, x >= y);

    printf("%d <= %d is %d \n", x, t, x <= t);

    return 0;
}
Relational Operators in C
Relational Operators in C

Logical Operators

Operator       Equal Expression                                         Example

&&                  True only if all operands are true                 if x=4 and y=2, ((x==4) && (y>5)) equals to 0

||                    True only if either one operand true          if x=4 and y=2, ((x==4) || (y>5)) equals to 1

!                      True only if the operand is 0                         if x=4 , the expression !(x==4) equals to 0

Bitwise Operators

Operator         Equal Expression

&                     Bitwise AND

|                      Bitwise OR

^                      Bitwise Exclusive OR

~                      Bitwise Complement

>>                    Shift Right

<<                    Shift Left

Leave a Reply

Your email address will not be published. Required fields are marked *