Operators

Operators

An operator is an symbol used to perform operations in programming 👉[+, -, *, /]
Ex: 👉3+4 , hence here (+)is an operator.

Types of operators

*Arithmetic Operators
*Relational Operators
* Logical Operators
*Bitwise Operators
*Assignment Operators

Arithmetic Operators

Operator

 

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulus


move on to code👇
#include <stdio.h>

int main()
{
    int ab;
    a = 34;
    b = 12;

    printf("a + b = %d\n"a+b);
    printf("a - b = %d\n"a-b);
    printf("a * b = %d\n"a*b);
    printf("a / b = %d\n"a/b);
    printf("a / b = %d\n"a%b);
    return 0;
}

Relational Operators

Operator

Description

==

Is equal to

!=

Is not equal to

> 

Greater than

< 

Less than

>=

Greater than or equal to

<=

Less than or equal to


move on to code👇
#include <stdio.h>

int main()
{
    int ab;
    a = 34;
    b = 34;

    printf("a + b = %d\n"a==b);
    return 0;
}


Logical Operators

Operator

description

Example

&&

Logical AND operator. If both the operands are non-zero, then the condition is true.

(A && B) is false

||

Logical OR operators. If any of these two operands is non-zero, the condition became true.

(A || B) is true

!

Logical NOT operator. It is used to reserve the logical state of its operand. If condition is true, then Logical NOT operator will make it false.

!(A && B) is true


move on to code👇
#include <stdio.h>

int main()
{
    int ab;
    a = 34;
    b = 34;

    printf("a && b = %d\n"a&&b);
    return 0;
}
it will return ðŸ‘‰1 (because the value of integer is true)
where as,
#include <stdio.h>

int main()
{
    int ab;
    a = 0;
    b = 34;

    printf("a && b = %d\n"a&&b);
    return 0;
}
it will return ðŸ‘‰0 (because the value of integer is false)


Bitwise Operators

a

b

A & b

A | b

A ^b

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

1

1


move on to code👇
#include <stdio.h>

int main()
{
    int ab;
    a = 2;
    b = 3;

    printf("a + b = %d\n"a & b);
    return 0;
}
it will return ðŸ‘‰2 (because 0 and 1 = 2), first it will convert it into binary form, and work as bit by bit.

Other bitwise operators

~ is the binary one's complement operator.
<< is the binary left shift operator.
>> is the binary right shift operator.


Assignment Operators

operator

Description

=

Single assignment operator. Assign values from right side operand

+=

Add AND assignment operator. It adds the right to left operand and assigned the result to the left operand.

-=

Subtract AND assignment operator. It adds the right to left operand and assigned the result to the left operand.

*=

 Multiplication AND assignment operator. It adds the right to left operand and assigned the result to the left operand.

/=

Division AND assignment operator. It adds the right to left operand and assigned the result to the left operand. 


Miscellaneous operators

Operator

Description

Example

Sizeof()

Return the size of variable

Will return int’s size

&

Returns the address of variable

Returns the actual address of variable

*

Pointer to variable

*a:

?:

Conditional expression

If condition true(X)

Otherwise(Y)



Operator precedence in C


Category

Operator

Associativity

Postfix

() [] ->,++ - -

Left to Right

Unary

+ - ! ~ ++ - - (type)* & sizeof

Right to Left

Multiplicative

* / %

Left to Right

Additive

+ -

Left to Right

Shift

<<>> 

Left to Right

Relational

<<= >> =

Left to Right

Equality

== !=

Left to Right

Bitwise AND

&

Left to Right

Bitwise xOR

^

Left to Right

Bitwise OR

|

Left to Right

Logical AND

&&

Left to Right

Logical OR

||

Left to Right

Conditional

?:

Right to Left

Assignment

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

Right to Left

comma

.

Left to Right


note: In programming languages, The associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses.
    This is basically based on [BODMAS].




Comments

Popular Posts