C operators are used in programs to trigger an action i.e. to perform certain mathematical or logical manipulations.
C operators are classified as follows:
Arithmetic operators available in c are as follows:
|Arithmetic Operator Symbol||Description||Example|
|%||Performs modular division(Gives remainder after performing division)||11%2=1|
|+(Unary Plus)||Gives value of operand||+5=5|
|-(Unary Minus)||Changes sign of its operand||-5|
All relational operators gives answer in either TRUE or FALSE. Every non zero value is consider as TRUE by the compiler.
Relational operators available in c are as follows:
|Relational Operator Symbol||Meaning||Example|
|==||Comparison(equality)||2==2 (gives TRUE)|
|>||Greater than||5>6 (gives FALSE)|
|>=||Greater than or equal to||5>=5 (gives TRUE)|
|<=||Less than or equal to||2|
|!=||Not equal to||5!=2 (gives TRUE)|
Unlike relational operators that establish relationship among values, logical operators refers to the ways these relationships among values can be connected.
There are three logical operators in c:
|Logical Operator Symbol||Meaning||Example||Result|
|&&||AND (If anyone condition is False, answer is False)||6>=0 && 6>=20||False|
|||||OR (If anyone condition is True, answer is True)||2>=1 || 1>=5||True|
|!||NOT (It negates value of its operand and is an unary operator)||!(6<=6)||False|
Assignment operators are used to assign the result of an expression or a particular value to a variable.
The symbol of assignment operator is “=”.
For example, a=11 will assign value 11 directly in a, and x=a+b will assign value of expression a+b in x.
C also has a set of shorthand assignment operators. Consider this statement,
The above statement can also be written using shorthand assignment operators as
|Statement with simple assignment operator||Statement with shorthand assignment operator|
Increment & Decrement Operators
C also provides the unary increment operator(++) and the unary decrement operator(–).
The increment operator adds 1 to its operand whereas the decrement operator subtracts 1 from its operand.
We can use increment and decrement operators in anyone of the following form:
Postfix: If increment and decrement operators are placed after a variable, they are said to be postfix increment or decrement operators.
Postincrementing or postdecrementing the variable causes current value to be used in expression in which it appears, then variable value is incremented or decremented.
Prefix: If increment and decrement operators are placed before a variable, they are said to be prefix increment or decrement operators.
Preincrementing or predecrementing the variable causes current value to be incremented or decremented first, and then only the variable value is used in the expression.
The rules can be summarized as:
- Prefix: Change then use
- Postfix: Use then change
The only ternary operator available in c is “?:” to costruct conditional expression.
The expression_1 is a logical or relational condition which is evaluated first. If the evaluated value is TRUE, then the value of complete expression will be expression_2 otherwise, it will be expression_3.
Special operators available in c are as follows:
The comma operator is used to link the related expression together. Since, comma operator has the lowest precedence over other C operators, therefore it is always given in brackets.
It is evaluated from left to right, and the evaluated rightmost value is the value of combined expression.
In the above example, first 10 is assigned to a, then a is incremented by 1, and finally 11*2 is assigned to a.
The sizeof operator is a compile time operator which returns the number of bytes that the operand occupies. The operand may be a variable, constant or a data type.
where var is a declared variable and type is a valid c data type.
a=sizeof(sum); b=sizeof(long int); c=sizeof sum;
There are also six bitwise operators available in c:
- Bitwise AND Operator (&)
- Bitwise OR Operator (|)
- Bitwise Exclusive OR Operator (^)
- Ones Complement Operator (~)
- Left Shift Operator (<>)
- Right Shift Operator (>>)