C supports a wide variety of operators, such as +, -, *, /, &, <, > etc.,
An Operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables.
C operators can be classified into a number of categories. They are :
- Arithmetic operators.
- Relational Operators.
- Logical Operators.
- Assignment Operators.
- Increment and Decrement Operators.
- Conditional Operators.
- Bitwise Operators.
- Special Operators.
Arithmetic operators include +, -, *, /, %, which performs all mathematical manipulations. These operators can operate on any built-in data type allowed in C. Table shows the function of Arithmetic Operators.
|+||Addition or unary plus|
|-||Subtraction of unary minus|
Relational operators are used to compare two operands, and depending on their relation, certain decisions are taken. For example, it can be used to compare the age of two persons, price of two items and so on. There are 6 types of relational operators. They are:
|<||Is less than|
|>||Is greater than|
|<=||Is less than or equal to|
|>=||Is greater than or equal to|
|==||Is equal to|
|!=||Is not equal to|
C supports three Logical Operators. They are:
Assignment operators are used to assign the result of an expression to a variable. Usually, ‘=’ operator is used. There is an additional ‘shorthand’ assignment operators of the form
V op = exp;
exp = expression and
op = a binary arithmetic operator.
The Operator op= is known as the shorthand assignment operator.
Shorthand Assignment Operators
|Statement with simple assignment operator||Statement with shorthand operator|
|a = a + 1||a += 1|
|a = a - 1||a -= 1|
|a = a * (n+1)||a *= n + 1|
|a = a / (n+1)||a /= n + 1|
|a = a % b||a % = b|
Increment and Decrement Operators:
C supports two unique operators that are not found in other languages. They are: ++ and -- (increment and decrement operators respectively).
The operator ++ adds 1 to the operand, while – subtracts 1. Both are unary operators and take the following form:
++m; or m++;
--m; or m--;
++m is equivalent to m = m + 1;
--m is equivalent to m = m – 1;
A ternary operator pair “? :” is available in C to construct conditional expressions of the form:
Exp1? Exp2: Exp3
Exp1, Exp2 and Exp3 are expressions. The operator ?: works as follows:
Exp1 is evaluated first. If it is nonzero (true), then exp2 expression is evaluated and becomes the value of the expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the expression. Here only one of the expressions is evaluated.
Bitwise operators are special operators that are used for manipulation of data at the bit level. These operators are used for testing the bits, or shifting them to right or left. Bitwise operators may not be applied to float or double.
|^||Bitwise exclusive OR|
C supports some special operators such as :
- comma operator
- sizeof operator
- pointer operators(& and *)
- member selection operators.
C Programming: Tips of the Day
What is the difference between exit(0) & exit(1) and exit(0) & exit(1) in C?
exit(0) indicates successful program termination & it is fully portable, While
exit(1) (usually) indicates unsuccessful termination. However, it's usage is non-portable.
Note that the C standard defines EXIT_SUCCESS and EXIT_FAILURE to return termination status from a C program.
0 and EXIT_SUCCESS are the values specified by the standard to indicate successful termination, however, only EXIT_FAILURE is the standard value for returning unsuccessful termination. 1 is used for the same in many implementations though.
Ref : https://bit.ly/3l4TAjG
- Exercises: Weekly Top 16 Most Popular Topics
- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools