Operators in C Programming

No comments
In this lesson we will discuss about what the meaning of operator in C programming language and and its types.Like mathematics, also in C programming we are required to do various operations between variables,numbers and constants and the symbols or signs which are used for doing these operations are called operators.So operators are the signs or symbols which tells the compiler to perform specific operations between two or more operands(variables,constants etc.).
For example if we want to add two variables say 'x' and 'y' then we will use a arithmetic operator '+', and for this x and y are operands.In c programming language these operators are divided into different categories.

Types of operators in c programming language-

Arithmatic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators

Arithmatic Operators

Arithmetic operators are used for mathematical calculations between two variables.Here is a list of arithmetic operators.Alongside the explanation we have provided some examples for each operator and two variables A and B by assuming the values of A and B 6 and 2 respectively.
Operator symbol Description Example
'+' Used for adding two or more then two operands A+B=8
'-' Used for subtracting operand on right side from the operand on the left side of the operator. A-b=4
'*' Used for multiplying two operands A*B=12
'/' This operator is used for dividing. A/B=3
'%' This is modulus operator and returns the remainder after the division of two integers. A%B=0
'++' It is an increment operator and used to increase the value of any integer by 1. B++=7
'--' It is a decrement operator and used to decrease the value of the integer by one. B--=5

Relational Operators

Relational operators are used for comparison,and always returns boolean value.Here is a list of some relational operators supported by C programming language.
Operator symbol Description Example
'==' Compares the values of two operands and returns true if values are equal and returns false if the values are not equal. (A==B) returns false
'!=' Compares the values of two operands and returns false if values are equal and returns true if the values are not equal. (A!=b) returns true
'<' Compares the vlues of two operands and returns true if the left operand is less then the right one and false if the left operand is greater then the right one. (A<B) returns true
'>' Compares the vlues of two operands and returns true if the left operand is greater then the right one and false if the left operand is less then the right one (A>B) returns false
'<=' Compares the vlues of two operands and returns true if the left operand is less then or equal to the right one and false if the left operand is greater then the right one (A<=B) returns true
'>=' Compares the vlues of two operands and returns true if the left operand is greater then or equal to the right one and false if the left operand is less then the right one (A<=B) returns false

Logical Operators

There are three logical operators in C programming language, which are mentioned below-
Operator symbol Description Example
'&&' This is called AND operator and returns true if both the conditions written on both sides of the operator are true,and returns false if any of the condition is not true. (A>5)&&(b>5) returns false
'||' This is called OR operator and returns true,if any one of the both conditions written both sides of the operator is true, and returns if both the conditions are false. (A>5)||(B>5) returns true
'!' This operator is called NOT operator and reverse the logical state of the operand,if a condition is true then this will reverse it into false. !(A>B) returns false

Bitwise Operators

Bitwise operators is an advance topic in C programming. Bitwise operators are used to work at bit level.Here are some bitwise operators used in C programming.
Operator symbol Description
& This is bitwise and operator
| This is bitwise OR operator
^ This is bitwise exclusive OR
~ This is bitwise exclusive complement
>> Shift right
<< Shift left

Assignment Operators

Assignment operators are used to assign some value to the variable.'=' is the most common assignment operator used in most of the languages,it assigns the valu at right side of it to the right side of the operator.Here are some assignment operators used in C programming language.
Operator symbol Description Example
'=' This operator assigns value of right operand to the left operand A=25; value of a is assigned 25
'+=' This assignment operator adds the right operand to the left operand and assignes the total value to the left operand A+=b; this means A=A+b
'-=' This operator subtracts the right operand from left operand and then assigns the total value to the left operand. A-=b; it means A=A-b;
'*=' This is multiply AND assignment operator used to multiply the both operand and the assign it to the left operand. A*=b; it means A=A*b;
'/=' This is called divide and assignment operator.It divides the left operand by right operand and then assigns the value to the left operand. A/=b; it means A=A/b;
'%=' This is called modulus AND operator. This operator first takes the value of (left operand)%(right operand) and then assigns the value to the left operand. A%=b; it means A=A%b;
'<<=' This is left shift AND assignment operator. A<<=b; it means A=A<<b
'>>=' This is right AND assignment operator. A>>=b; it means A=A>>b
'|=' This is called bitwise inclusve OR and assignment operator. A|=b; it means A=A|b
'^=' This is called bitwise Exclusive OR and assignment operator. A^=b; it means A=A^b;
'&=' This is called bitwise AND assignment operator A&=b; it means A=A&b;

Misc Operators

Except the operators we discussed above, there are some other important C operators.
Operator symbol Description Operator symbol
'*' This is shows pointer to a variable. *a
'?:' Used to check condition If true condition? value a:otherwise b
'&' This is used to return the address of the variable &v; it returns the actual address of the variable.
'sizeof()' This operator is used to return the size of the variable. sizeof(x);it will return the size of the variable x written inside the parenthesis.
Here we read about different type of operators used in C programming language.In our next article we will read about decision making statements in C programming language.

No comments :

Post a Comment