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.
Previous: Constants, Variables, and Data Types
Next: C printf()
C Programming: Tips of the Day
What does (x ^ 0x1) != 0 mean?
The XOR operation (x ^ 0x1) inverts bit 0. So the expression effectively means: if bit 0 of x is 0, or any other bit of x is 1, then the expression is true.
Conversely the expression is false if x == 1.
So the test is the same as:
if (x != 1)
and is therefore (arguably) unnecessarily obfuscated.
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook