Operators in C | C Programming Operators |C-Operators

 

An operator can be defined as a symbol that specifies an operation to be performed. C language supports a rich set of operators. Operators help the programmer to perform computation on values.

An operator is a symbol that tells the compiler to perform certain mathematical or logical computations. The data items on which the operators act upon are called operands. Some operators require a single operand to perform operation while others might require two operands.

Arithmetic operators are the most common. Other operators are used for comparison of values, combination of logical states, manipulation of individual binary digits etc.


Categories of operators:

Operators in C can be classified into a number of categories as follows:

1

Assignment operator

=

2

Arithmetic operators

+, -, *, /, %

3

Relational operators

==, !=, <=, >=, <, >

4

Logical operators

&&, ||, !

5

Increment/Decrement operators

++, —

6

Conditional operator

?:

7

Bitwise operators

&,  |,  ^,  << , >>,  ~

8

Other operators

Address operator &

Pointer operator(arrow)->

Indirection operator *

 

Assignment Operator:

An assignment operator is used to assign a value to a variable or to assign the result of an expression to a variable. The = symbol is used as an assignment operator.

Or simply we can say, a data or value can be stored in a variable with the help of assignment operator. The assignment operator is used in assignment statement and assignment expression.

The syntax of an assignment is,

Variable name=expression;

For example,         a=5;

                             Sum=a+b;

The above statement assigns the total of the contents of variables a and b to a variable named, sum. The variable sum appears to the left of the equal sign, which is called the assignment operator. On the left of this operator only a single variable can occur. Expressions and constants can appear only on the right hand side of assignment operator.

The assignment operator can be used in three ways. They are single assignment, multiple assignment and compound assignment.

When we store one value in one variable it is single assignment.

For example, a=10;

When we store one value in many variables it is multiple assignments.

For example, x=y=z=20;

Similarly, using the arithmetic operators with assignment operator is compound assignment. The compound assignment operators are: +=, -+, *=, /=, %=

 

Single assignment

Compound assignment

a=a+5;

a+=5;

b=b-10;

b-=10;

c=c/2;

c/=2;

x=x*y;

x*=y

year=year%4

year%=4;

 

 

Arithmetic Operators:

The arithmetic operators are used to perform mathematical calculations such as addition, subtraction etc. C supports all the common arithmetic operators.

Operator

Meaning

+

Addition

Subtraction

*

Multiplication

/

Division

%

Modulus

These operators can operate on any built –in data types such as int, char and float. However, the modulus operator requires an integral operand (must not be float).

 

Relational Operators:

These are used to compare the values two variable or constants. The relational operators are symbols that are used to test the relationship between variables or between a variable and a constant.

For example,
(salary==5000)

Here == is the operator that is used to test equality.        

 There are six relational operators provided by C. They are:

 

Operator

Meaning

==

equal to

!=

not equal to

greater than

less than

>=

greater than or equal to

<=

less than or equal to

 

Relational operators are used in expressions to compare the values of two operands. If the result of comparison is true, the value of the expression is one. If the result of comparison is false, the value of the expression is zero.

For example, Let the two variables a and b have initial values 30 and 35 respectively. The following table illustrates the usage of the relational operators:

Expression

Result

a>20

True

a+b>=70

False

a<b

True

a<=b

True

a+15==b

False

a!=20

True

 

Logical Operators:

Logical operators are symbols that are used to combine two or more expressions containing relational operators. This situations will arise when we want to perform a statement block on a combined condition, like x>5 and x<10. Then we code this using an AND operator. The AND operator is represented as &&.

Example:

((x>5)&&(x<10))

(Salary>=10000&&salary<=20000)

The logical operators are used to combine conditions. These operators are used with one or more operand and return either value zero (for false) or one (for true). The operand may be constant, variables or expressions. And the expression that combines two or more expressions is termed as logical expression.

C has three logical operators:

Operator

Meaning

&&

AND

||

OR

!

NOT

Where logical NOT is a unary operator and other two are binary operator. Logical AND gives result true if both the conditions are true, otherwise result is false. And logical OR gives result false if both the condition false, otherwise result is true.

 

Increment and Decrement Operators:

The increment and decrement operators are two very useful operator used in C. Both increment and decrement operator are used on a single operand and variable, so it is called as a unary operator.

 

Increment Operator:

Increment operators are used to increase the value of a variable by 1. This operator is represented by ++ symbol. The increment operator can either increase the value of the variable by 1 before assigning it to the variable or can increase the value of the variable by 1 after assigning the variable. Thus it can be classified into two types:

    1.     Pre-increment operator

    2.     Post-increment operator

Pre-increment operator (Prefix increment operator):

A pre-increment operator is used to increment the value of a variable before using it in an expression. In the pre-increment, first increment the value of variable and then used inside the expression.

Syntax:      ++variable;

For example:    x=++i;

In this case, the value of i will be incremented first by 1. Then the new value will be assigned to x for the expression in which it is used.

Post –increment operator (Postfix increment operator):

 A post increment operator is used to increment the value of the variable after executing the expression completely in which post increment is used. In the post-increment, first value of variable is used in the expression and then increment the value of variable.

 Syntax:      variable++;

For example:    x=i++;

In this case, the value of i is first assigned to x and after that, i is incremented.

Decrement Operators:

We use decrement operators in C to decrement the given value of a variable by 1. This operator is represented by – – symbol.

Decrement operator can be classified into two types:

     1.     Pre-decrement operator

     2.     Post-decrement operator

 

Pre-decrement operator (Prefix-decrement operators):

The pre-decrement operator first decrement the value of variable and then used inside the expression.

Syntax:      – – variable;

Example:    x= – – i;

Here, value of i will decrement first by 1 and then assign it to the variable x.

 

Post-decrement operator (Postfix-decrement operators):

In post-decrement first value of variable is used in the expression and then decrement the variable.

Syntax:      variable – –

Example:   x=i – – ;

Here, the value of i is first assigned to x and after that, value of i is decremented.

 

Conditional Operators:

Sometimes we need to choose between two expressions based on the truth value of a third logical expression. The conditional operator can be used in such cases. It sometimes called as ternary operator. It required three expressions as operand and it is represented as ?  :

Syntax:      exp1 ? exp2 : exp3;

Here exp1 is first evaluated. It is true then value return will be exp2 . If false then exp3.

Example:   c=(a>b)?a:b;

Where: a and b are operands

         ?:    is the conditional operator

         >    is the relational operator

If the condition is true then value a is assigned to c otherwise b is assigned to c.

 

Programming Example:

int main()

 {

int a=10, b=2;

 int s= (a>b) ? a:b;

printf(“value is:%d”,s);

return 0;

}

Output: Value is: 10 

 

Bitwise Operators:

For manipulating data at the bit level, C provides us special operators known as bitwise operators. The bitwise operators are the operators used to perform the operations on the data at the bit level. These operators are used to perform bitwise operations such as for testing the bits, shifting the bits to left or right, one’s complement of bits etc.

These operators can be applied only on int and char data types but not on float and double data types. Using bitwise operators, we can manipulate each and every bit of memory. Since we are doing it in bit level the operation will be much faster and efficient.

These operators help us to directly manipulate individual bits within a word. Bit level manipulations are used in setting a particular bit or group of bits to 1 or 0. It is mainly used in numerical computations to make the calculation process faster.

Various bitwise operators in C:

Operators

Meanings

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

(Exclusive OR)

<< 

Shift left

>> 

Shift right

~

one’s complement

Example:

a       b                 a&b            a|b              a^b

 1       1                 1                 1                 0

1       0                 0                 1                 1

0       1                 0                 1                 1

0       0                 0                 0                 0

Programming Example:

Program to test whether a number is odd or even

Output:

Example:

main()

{

          int i;

          i=4;

i=i<<1;

printf(“\n%d”,i);

i>>1;

printf(“\n%d”,i);

getch();

}

Output:

8

 4            

Leave a Comment