C Programming/Operators: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Line 55: Line 55:
== Logical Operators ==
== Logical Operators ==
'''C''' provides three logical operators when we test more than one condition to make decisions. These are: <code>&&</code> (meaning logical AND), <code>||</code> (meaning logical OR) and <code>!</code> (meaning logical NOT).
'''C''' provides three logical operators when we test more than one condition to make decisions. These are: <code>&&</code> (meaning logical AND), <code>||</code> (meaning logical OR) and <code>!</code> (meaning logical NOT).
{| class="wikitable"
|-
! Operator !! Description
|-
| && || And operator. Performs a logical conjunction of two expressions. (if both expressions evaluate to True, result is True. If either expression evaluates to False, result is False)
|-
| &#124;&#124; || Or operator. Performs a logical disjunction on two expressions. (if either or both expressions evaluate to True, result is True)
|-
| ! || Not operator. Performs logical negation on an expression.
|}


== Bitwise Operators ==
== Bitwise Operators ==

Revision as of 04:04, 5 February 2018

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators:

Arithmetic Operators

Arithmetic Operators are used to performing mathematical calculations like addition +, subtraction -, multiplication *, division / and modulus %.

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus

Increment and Decrement Operators

Increment and Decrement Operators are useful operators generally used to minimize the calculation, i.e. ++x & x++ means x=x+1 or --x & x−− means x=x-1. But there is a slight difference between ++ or −− written before or after the operand. Applying the pre-increment first add one to the operand and then the result is assigned to the variable on left whereas post-increment first assigns the value to the variable on left and then increment the operand.

Operator Description
++ Increment
−− Decrement

Relational Operators

Relational operators are used to compare two quantities or values.

Operator Description
== Is equal to
!= Is not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Logical Operators

C provides three logical operators when we test more than one condition to make decisions. These are: && (meaning logical AND), || (meaning logical OR) and ! (meaning logical NOT).

Operator Description
&& And operator. Performs a logical conjunction of two expressions. (if both expressions evaluate to True, result is True. If either expression evaluates to False, result is False)
|| Or operator. Performs a logical disjunction on two expressions. (if either or both expressions evaluate to True, result is True)
! Not operator. Performs logical negation on an expression.

Bitwise Operators

C provides a special operator for bit operation between two variables.

Assignment Operators

Assignment operators applied to assign the result of an expression to a variable. C has a collection of shorthand assignment operators.

Conditional Operator

C offers a ternary operator which is the conditional operator (?: in combination) to construct conditional expressions.

Special Operators

C supports some special operators