LEARN C PROGRAMMING LANGUAGE
Operators in a C Program
In this post, we will learn about the operators in the C Program and their usage in the C Program.
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 and variables in C Programming Language.
The operators can be Arithmetic operators, relational operators, logical operators, assignment operators, increment and decrement operators, conditional operators, bitwise operators and special operators.
Operators can be categorized into two types:
- Operators based on the number of operands and
- Operators based on their operations.
Operators based on the number of operands
Operands are the data or variables on which the operator performs the specified operation. At least one operand is required by each operator to perform an operation.
The types of operators classified on the basis of the required number of operands are as follows:
1. Unary operators: The operators that require only one operand to perform the operation are known as unary operators. The operator can appear either on the left side or on the right side of the operand in some cases such as increment operator (++), decrement operator (–).
Other operators are:
|!||logical negation (NOT)|
|~||bitwise 1’s complement|
2. Binary operators: The operators that work on two operands are known as binary operators. The operator is placed between these two operands, it can be a variable or a constant.
For most of the Arithmetic and logical operations, binary operators provided by the C language are used. These includes =(equals), +(plus), -(minus), *(multiply), /(divide), %(modulus), <(less than), >(greater than), &&(and), ||(or) <=(less than equal to), >=(greater than equal to), !=(not equal to), ==(equals to) and many more.
The syntax for using these operators is:
Operand1 binary-operator operand2
3. Ternary operators: The operators that operate on three operands are known as ternary operators. The C Pragramming language provides that conditional operator ( ? : ) as the ternary operator. Conditional operator is used for making a decision.
The syntax of the conditional operator is:
(Condition? Statement 1: Statement 2)
The output of the conditional operator is statement 1 if the condition is true or statement 2 if the condition is false.
For Example: a>b?”yes”:”no”
Let us assume a=4 and b=5 so the output will be “no” as b is greater than a and the condition a>b is false.
In the next post, we will learn about the operators based on the operations.