What are Operators in C programming

What are operators in C?

Declaration gives the list of variables with a different type of values to be used

The operator is something which tells what to be done to the declared variables.

Every operator in c has

  • Priority
  • Associativity

Why  6/5 gives  1 not 1.2 in C program?

Do you know? if we calculate 6/5 gives 1 not 1.2 in C. first of all,4/8 gives 1 not 0.5. second, Do you know why? third, if values are integers it evaluate as int

if one value is int and another value is float then it evaluates the result as float.

6.5/5.5 gives 1.1818

Have you ever checked arithmetic in c. if not, you can check now

#include <stdio.h>
int main()
{
int k;
k=66/30;
printf("%d",k);
    return 0;
    }

Rendered by QuickLaTeX.com

Observe the results carefully. plus, notice the arithmetic in C is different, if we compared with normal arithmetic.

if both the values are integers, C evaluate result as int not float

                        
 Precedence and order of evaluation table

Rendered by QuickLaTeX.com

Arithmetic Operators

Rendered by QuickLaTeX.com

Note: the modulus  % and divide / is not same, % gives remainder and divide / gives quotient

Rendered by QuickLaTeX.com

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x=6;
    int y=2;
    int modulus;
    int divide;
    
    {
        modulus=x%y;
        divide =x/y;
    }
        printf(" modulus=%d \n",modulus);
        printf(" divide=%d \n",divide);

    return 0;
}

Output

modulus = 0
divide = 3

Logical Operators

Logical NOT(!)

Rendered by QuickLaTeX.com

Logical OR

Rendered by QuickLaTeX.com

Logical Operators

Rendered by QuickLaTeX.com

Equality Operators

Rendered by QuickLaTeX.com

Relational Operator

Rendered by QuickLaTeX.com

Unary operator’s

Unary operators are designed for single operands and There are three unary operator’s

Increment operator and decrement operator

OperatorMeaning
++Increase value by 1
– –Decrease value by 1
VariantsExample
Prefix++ x or – -x
Postfixx+ + or x- –
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int  e, f,g,h, a=2, b=2, c=2,d=2; //Initializing variables
    e = a++; //postfix
    f = b--; //postfix
    g = ++c; //prefix
    h = --d; //prefix

    printf("e=%d f=%d g=%d h=%d",e,f,g,h); //Print
    return 0;
}
Output
e=2  f=2 g=3 h=1
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int   a=2; //Initializing variable
    printf("a=%d",a++);
    printf("a=%d",a); //Print
    return 0;
}
Output
a=2 a=3

Note: a++(postfix increment) and ++a(prefix increment) is not same similary  a – -(postfix Decrement) and  – -a(prefix Decrement) is not same  

In prefix expression : the operator(++a (or) – -a) is applied in front of operand(a)

Rendered by QuickLaTeX.com

In postfix expression : the operator(++a (or) – -a) is applied after the operand(a)

Rendered by QuickLaTeX.com

Unary Minus(-)
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int c, a=2, b=2; //Initializing variable a with 2, variable b with 2 and c 
    c =(a+b); //Add the two variable 
    c=-(c); //Unary Minus
    printf("%d",c); //Print C variable 
    return 0;
}
Output
-4
Conditional operator(?:)

Syntax

exp1 ? exp2:exp3
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int   x,y; //Initializing variable
    scanf("%d",&x);
    y=(x>4?2:5);
    //printf("a=%d",a++);
    printf("y=%d",y); //Print
    return 0;
}
Output
if  x>4 then y=2
if x<4 then y=5

sizeof Operator

The sizeof Operator is designed to give the size of an operand

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int   x=100, result; //Initializing variable
    result=sizeof(x);
    printf("result=%d",result); //Print
    return 0;
}
Output
result =4

Bitwise operators

Bitwise operators work on bit level not on byte( int, char)

Bitwise AND (&)

Truth table

Rendered by QuickLaTeX.com

find a&b, if b=4, a=4

Rendered by QuickLaTeX.com

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=4, b=4, c;
    c= a&b;
    printf("c=%d", c);
    return 0;
}
Output
c= 4

Remember & and && is not the same because & is a bitwise operator and && is a logical operator

Bitwise OR(|)

Truth table

Rendered by QuickLaTeX.com

what is a|b? if a=4, b=4

Rendered by QuickLaTeX.com

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=4, b=4, c;
    c= a|b;
    printf("c=%d", c);
    return 0;
}
Output
c=4

Bitwise XOR(^)

Truth table

Rendered by QuickLaTeX.com

find the value of a^b, if a=4, b=4

Rendered by QuickLaTeX.com

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=4, b=4, c;
    c= a^b;
    printf("c=%d", c);
    return 0;
}
Output
c = 0

Bitwise NOT(~)

The ~ operator designed to perform only on one variable

Rendered by QuickLaTeX.com

find the value of ~a, if a=4

Rendered by QuickLaTeX.com

Rendered by QuickLaTeX.com

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=4, c;
    c= ~a;
    printf("c=%d", c);
    return 0;
}
Output
c=-5

Bitwise leftshift(<<)

solve the left shift (<< 2) of a, when a=4

Rendered by QuickLaTeX.com

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=4, c;
    c= a<<2;
    printf("c=%d", c);
    return 0;
}
Output
c =16

Bitwise right shift(>>)

find right shift(>>2) of a, if a=4

Rendered by QuickLaTeX.com

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=4, c;
    c= a>>2;
    printf("c=%d", c);
    return 0;
}
Output
c=1

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.