C Language

Operators In C

In C Language by Baqir AliLeave a Comment

C has a rich set of operators (i.e., things like + – * / ), which allow you to write complicated expressions quite compactly. General expressions are formed by joining together constants and variables (operands) via various operators.

Operators in C fall into a number of classes:

Arithmetic operators, Unary operators, Relational and Logical operators, Assignment operators, Equality operators, and the Conditional operator.\

Unary Operators

Unary operators are operators that only take one argument.

+123  positive 123
-123  negative 123
!i    logical negation (i.e., 1 if i is  zero, 0 otherwise)
++i   adds one to i, and returns the new value of i
––i   subtracts one from i, and returns the new value of i
i++   adds one to i, and returns the old value of i
i– –  subtracts one from i, and returns the old value of i 

Binary Operators

Binary operators work on two operands (‘binary‘ here means 2 operands, not in the sense of base-2 arithmetic).

+   addition
–   subtraction
*   multiplication
/   division
%   remainder (e.g., 2%3 is 2), also called  'modulo'
<<  left-shift (e.g., i<<j is i shifted to the left by j bits)
>>  right-shift
&   bit wise AND
|   bit wise OR
^   bit wise exclusive-OR 
&&  logical AND (returns 1 if both operands are non-zero; else 0)
||  logical OR (returns 1 if either operand is  non-  zero; else 0)
<   less than (e.g., i<j returns 1 if i is less than j)
>   greater than
<=  less than or equal
>=  greater than or equal ==   equals
!=  does not equal
?   conditional operator 

Assignment Operators

The most common assignment operator in C is the equals operator, =. It is used to change the value of a variable. For instance, the expression:  f = 3.4; causes the floating-point value 3.4 to be assigned to the variable f.

Multiple assignments are permissible in C. For example,

i = j = k = 4;

causes the integer value 4 to be assigned to i, j, and k, simultaneously.

Assignment operators are really just binary operators.

=   assignment
+=  addition assignment
-=  subtraction assignment
*=  multiplication assignment
/=  division assignment
%=  remainder/modulus assignment
&=  bit wise AND assignment
|=  bit wise OR assignment
^=  bit wise exclusive OR assignment
<<= left shift assignment
>>= right shift assignment 

Arithmetic Operators

There are four main arithmetic operators in C:

   addition         +
   subtraction      –
   multiplication   *
   division         /  

There is no built-in exponentiation operator in C . Instead, there is a library function (pow) which carries out this operation.

e.g. x2 is represented as x * x

Operation C Operator Algebraic Expression C Expression
Addition + f + 7 f + 7
Subtraction a – b a – b
Multiplication * bm b * m
Division / x / y or x ÷ y x / y
Modulus % r mod s r % s

Relational Operators

Relational operators are used to compare two or more operators. The result of comparison is either true or false.

Symbol Operator Example
< Less Than a < b
> Greater Than a > b
= Equal to a  =  b
<= Less or Equal a <= b
>= Greater or Equal a >=b
<> Not Equal a <> b

Logical Operators

Effects  on relational expression’s results also AND, OR combines two or more Relational expressions,  Results will be returned either TRUE( 1 ) or FALSE ( 0 ) regards with specific situations.

Symbol Operator Example
AND AND A< b AND c > d
OR OR a <  b OR c > d
NOT  NOT   NOT ( a < b )

Suppose we have the following values:

a =  2, b = 5, c = 9, d = 7

Symbol Operator Example
AND a< b AND c > d 1
OR a <  b OR c > d 1
NOT   NOT ( a < b ) 0

Get The Coding Examples From C Language Code Example Page

Code Example

Support us by sharing this post

Leave a Comment