LEARN C PROGRAMMING LANGUAGE
Operators in a C Program
In this post, we will learn more about the operators in the C Program and their usage in the C Program.
Operators based on the Operations
Operator is a symbol which is used to perform certain computations or operations in a C Program. These operations can be arithmetic calculation, assignment on logical expressions. Operators usually operate on data/constants and variables in C Programming Language.Following are the various operators:
- Assignment operator:- The assignment operator is used to assign or store any value in a variable. The ‘equals to’ (=) operator is used for this purpose. The left side of the assignment operator can have only the variable name. But the right side can have a variable or a constant. The value at the right side of the assignment operator is stored in the left side operand. The syntax can be given as:
variable name = data/variable
Thus the value 9 is stored in x, 1.1 in average and the variable p has the same value as the value of the variable average i.e., p=1.1.
- Arithmetic operators:- To perform the arithmetic operations on data value the arithmetic operators are used. All the arithmetic operations can be carried out in C programming language such as:
Arithmetic operators are binary operators, they require two operands and the arithmetic operator is placed between these two operands. The syntax of the Arithmetic operators is:
operand1 arithmetic operator operand2
Usually, the arithmetic operator is used with the assignment operator so that the result of the arithmetic operation can be stored in a variable for further use, i.e.,
Variable = operand1 arithmetic operator operand2
- Comparison operators:- Comparison of data values is required for making a decision or for checking a condition, so C programming language provides Comparison Operators for this purpose. Comparison Operators also tell the relationship between the two operands thus they are also known as Relational Operators. The Comparison Operators supported by the C language are:
== equals to
< lesser than
> greater than
<= lesser than or equals to
>= greater than or equals to
!= not equals to
For performing a Comparison Operator two operands are required. The output of the comparison operator is a Boolean value i.e., either true or false accordingly. The syntax of the comparison operator is of the form:
operand1 comparison operator operand2
3<=9 returns TRUE
6>3+9 returns FALSE
- Logical operators:- Logical Operators are used to compare or evaluate a logical and relational expression. The expressions on which the logical operators are applied or reduced to “TRUE” or “FALSE” thus the logical operators are also known as Boolean Operators. The output of the logical operator is either 0 or 1 where 1 indicates the expression to be true and 0 indicates the expression to be false. The logical operators provided by the C language are:
&& logical and every Operand/ expression should be true for the output to be true else false.
x>y && z==2 The expression is TRUE if both the left and the right hand side expressions are true i.e., x is greater than y and z is equals to 2.
|| logical or if any one of the Operand/expression is true then the output is true else false.
a<2 || z>7 The expression is TRUE if any one of them (left side or right side expression) is true or if both of them are true.
! Logical not the negation of the Operand.
!(a==b) The expression is TRUE only if the value of a is not equal to the value of b.
- Increment/ decrement operators:- The increment/decrement operators are used to either increase or decrease the value of the operand by one only. These are unary operators.
The increment operator ++ increment by 1
The discriminant operator – – decrement by 1
These can be used in either postfix or prefix form. In the prefix form the operator is placed at the left side of the operand.
In the postfix form the operator is placed at the right side of the operand.
Let us assume that X has value 2.
print X -> 2
print X++ -> 2 (first print the value of x, then increase the value of x to 3)
print ++x -> 3 (first increase the value of x, then print the value of x)
We will understand the increment/decrement operators in detail with the help of examples later.
- Compound operators:- Compound Operators are the combination of the assignment and the arithmetic operators. First the Arithmetic operation is performed and then the assignment is done. These are basically written as the short form of assignment and arithmetic operator. Compound operators include:
=+ Add assignment
-+ subtract assignment
*+ Multiply assignment
/+ divide assignment
%= modulus assignment
The syntax of the compound operator is:
operand1 compound operator operand2
a+=2 is similar to a=a+2
b-=a is similar to b=b-a
This was all about the operators. In the next articles we will perform Complex calculations and evaluate complex expressions with the help of these operators.