Operators in C language
- Operators :
- Operators are the most important parts in any programming language. Without this we cannot write any program. Just as brick is required to build a house, similarly we need operators to make a program.
- What is the operators in C - C programming?
- The operator is a symbol that tells the compiler to perform any mathematical and logical operation. There are a lot of built-in operators in C programming.
- Such symbols which are used to perform any type of mathematical and logical operation. They are called operators in C programming. In programming, operators are used to manipulate data and variables.
- Operators work together with one or more variables, constants or operands. Variable, constant, operands, function and operators merge all these together to form an expression.
- A combination of operands and operators is called Expression. Operands are variables that together with operators perform certain operations. Let us understand Expression by example.
- Suppose an expression is given as "P + Q - R * 5 + 3". In this, +, -, * these are all operators while P, Q, R are all these variables which are also called operands. And both 5 and 3 are constant whose value does not change. And when Variables, Operators, Constant, function are all added, then expression is ready.
- The operator is a symbol that tells the compiler to perform any mathematical and logical operation. There are a lot of built-in operators in C programming.
- Such symbols which are used to perform any type of mathematical and logical operation. They are called operators in C programming. In programming, operators are used to manipulate data and variables.
- Operators work together with one or more variables, constants or operands. Variable, constant, operands, function and operators merge all these together to form an expression.
- A combination of operands and operators is called Expression. Operands are variables that together with operators perform certain operations. Let us understand Expression by example.
- Suppose an expression is given as "P + Q - R * 5 + 3". In this, +, -, * these are all operators while P, Q, R are all these variables which are also called operands. And both 5 and 3 are constant whose value does not change. And when Variables, Operators, Constant, function are all added, then expression is ready.
- Types of Operator in C
As you read above, in C programming there are lots of built-in operators which have been divided into different types. Which is as follows:
- Arithmetic Operator
- Assignment Operator
- Relational Operator
- Logical Operator
- Conditional Operator
- Bitwise Operator
- Increment/Decrements Operator
Let us now know about the operators in details one by one :
- Arithmetic Operator :
Arithmetic operators are the most basic operators. These operators are used to perform mathematical operations such as Addition ( + ), Subtraction ( - ), Multiplication ( * ), Division ( / ), Modulus ( % ).
The table given below lists all the Arithmetic operators as well as their descriptions.
- Arithmetic Operator
- Assignment Operator
- Relational Operator
- Logical Operator
- Conditional Operator
- Bitwise Operator
- Increment/Decrements Operator
Let us now know about the operators in details one by one :
- Arithmetic Operator :
Arithmetic operators are the most basic operators. These operators are used to perform mathematical operations such as Addition ( + ), Subtraction ( - ), Multiplication ( * ), Division ( / ), Modulus ( % ).
The table given below lists all the Arithmetic operators as well as their descriptions.
Operator | Description | Example |
+
|
use for addition of two operands
|
X+Y=3+5=8
|
-
|
use for substract of two operands
| X-Y=5-4=1 |
*
|
use for multiply of two operands
|
X*Y=5*4=20
|
/
|
use for divide of two operands
|
X/Y=20/2=10
|
%
|
use for modulus division of two operands
|
X%Y=5%2=10
|
2.Assignment Operator :
The Assignment operator is used to assign a value to a variable. Some operators come in this operators list, whose list is given in the table below.
Operator | Description | Example |
=
|
This operator is used to assign or
store the value in a variable.
|
A=8,A=4+4
|
+ =
|
This operator appends the right operand with the value of left operand and then assigns that value to the left operand, ie stores the left operand.
| A+=B is equal to A=A+B |
- =
|
This operator subtracts the right operand from the left operand and then assigns its value to the left operand.
|
A-=B is equal to A=A-B
|
* =
|
It multiply operator left operand with Right operand and then stores its result in left operand.
|
A*=B is equal to A=A*B
|
/ =
|
It divides operator left operand from right operand and then stores its result in left operand.
|
A/=B is equal to A=A/B
|
% =
|
It stores the modulus results of the two operands in the left operand.
|
A%=B is equal to A=A%B
|
3.Relational Operator :
Relational Operator is used to compare the value of two operands, hence it is also called Comparison operator. In the table below, lists of Relational Operators are given.
Operator | Description |
==
|
This operator is used to equal check two values. If both values are equal then it returns true.
|
! =
|
It is used to check that two operands are not equal. Meaning this operator is used to check the value of two operands, if both operands do not have equal value then it returns true.
|
Greater than(>)
|
It is used to check the value of the first operand greater than the value of the second operand. If the value of the first operand is greater than the value of the second operand then it returns true like (5> 2) return true
|
less than(>)
|
It is used to check the value of the first operand less than the value of the second operand. If the value
of the first operand is smaller than the value of the second operand then it returns true such as (3 < 4 ). |
>=
|
It is used to check the value of the first operand greater than and equal to the value of the second operand . If the value of the first operand is greater than or equal to the value of the second operand then it returns true as (5> = 5) return true
|
< =
|
This is used to check the value of the first operand less than the value of the second operand. If the value of the first operand is smaller than or equal to the value of the second operand then it returns true as (5 <= 5) return true
|
4.Logical Operator :
Logical operators are used to combine two expressions and a condition. These operators are used to perform logical operations. Let us understand by example.
For example, suppose vacancy has come out for a job and ITI and Diploma students have to be selected for that job, meaning either you must have an ITI course certificate or a diploma course certificate to do that job. Should be So now the person who has ITI qualification can also apply for that job and those who have done Diploma can also apply for that job. So here (OR) logic is being performed, meaning if you are one of the two, then you can do the job. This is called logical operation where work is done by logic.
Following are the list of logical operators which are shown in the table below.
Following are the list of logical operators which are shown in the table below.
Operator | Description | Example |
& & |
This operator is used to check the output of two expressions. In this, if the output both expressions is true then only it will return true otherwise False will return. Like A has value 5 store and B has value 3 store.
|
((P * Q) = = 15 ) & & ( ( P + Q ) = = 8 ) Return true
|
| | |
This operator is used to check the output of two expressions. In this, if one of the two expressions is output, it will return true, otherwise it will return False. Like A has value 5 store and B has value 3 store.
| ((P * Q) == 15) || ((P + Q) == 7) Return true |
! |
This operator is used to reverse any logical state. If a condition is true, then the Logical NOT operator makes it false. Like A has value 5 store and B has value 3 store.
|
! ((P * Q) == 15) && ((P + Q) == 8) Return false
|
5. Conditional Operator :
Conditional operators are used to check the condition. This operator has only two options TRUE and FALSE. If the given condition satisfies then TRUE will return otherwise FALSE will be return.
This operator is known by two names.
1. Ternary Operator
2. ?: Operator
It is actually if condition but it has been modified slightly and a short form has been created which can provide only two options. TRUE and FALSE.
Syntax of conditional operators
condition?true statement : false statement;
1. Question mark "?" Represents if part.
The condition part gives us either one or both types of value.
If the condition part returns true value, then true expression will be executed. That is, the expression on the left side of ":" will execute.
2.If the condition part returns false value then False expression will execute. That is, the expression on the right side of ":" will execute.
6. Bitwise Operator :
Bitwise operator is used to manipulate BIT level data. This operator is used for shifting from right to left and left to right bit. The bit-wise operator does not apply to float and double data types.
Computer only works on 0 and 1. The full form of Bit is a binary digit which is 0 and 1. The bitwise operator is calculated at 0 and 1. Whenever we manipulate the decimal number by bitwise operator, the processor first converts it to the form of 0 and 1 and then calculates it.
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR |
<< | left shift |
>> | right shift |
Truth Table of bit-wise operator & , | , ^ .
0 - false
1 - true
operation 1 | operation 2 | & | | | ^ |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
7. Increment / Decrement operator :
The Increment / Decrement operator is used to increase and Decrease the value of the variable once. For example, if the variable already has 6 value store, then its value will be reduced to 7 by increment operator and its value will be 5 by decrement operator.
Types of Increment / Decrement Operator :
1. Pre Increment / Decrement
2. Post Increment / Decrement
1. Pre Increment / Decrement :
In the pre increment / decrement operator, first the value increase and decrease occurs and then the further calculation is performed.
Syntax of Pre Increment / Decrement :
++(variable_name) ; - -(variable_name);
++(variable_name) ; - -(variable_name);
Example :
1. Post Increment / Decrement :
In the post increment / decrement operator, the calculation is first performed and then the value changes. Post means later. The value increase and decrease occurs after the calculus is performed.
Syntax of Pre Increment / Decrement :
(variable_name)++ ; (variable_name) - - ;
1. Post Increment / Decrement :
In the post increment / decrement operator, the calculation is first performed and then the value changes. Post means later. The value increase and decrease occurs after the calculus is performed.
Syntax of Pre Increment / Decrement :
(variable_name)++ ; (variable_name) - - ;
8. Special Operator :
The special operator is used to perform special work. There are a lot of special operators in C programming whose lists are given here.
Operator | Description | Example |
sizeof()
|
This operator returns the size of any variable according to its data type.
|
As ' a ' is an integer variable, it will return 4 or 2. According to different operating system
|
&
|
this operator return address
| &b; return address of b variable. |
*
|
The pointer variable is used to denote.
|
*b ;
|
No comments:
Post a Comment
If you have any query, please let me know