Operators

Operators

An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition.
C has a wide range of operators to perform various operations.

1. C Arithmetic Operators:-

An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).

Operator

Meaning of Operator

+addition or unary plus
-subtraction or unary minus
*multiplication
/division
%remainder after division (modulo division)

Example 1: Arithmetic Operators 


Output :

The operators +, - and * computes addition, subtraction, and multiplication respectively as you might have expected.
In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.
It is because both the variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after the decimal point and shows the answer 2 instead of 2.25.
The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder is 1. The % operator can only be used with integers.
Suppose a = 5.0b = 2.0c = 5 and d = 2. Then in C programming, 




2. C Increment and Decrement Operators:-


programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary, meaning they only operate on a single operand.

Example 2: Increment and Decrement Operators


Output

Here, the operators ++  and -- are used as prefixes. These two operators can also be used as postfixes like a++ and a-- .


3. Assignment Operators:-


An assignment operator is used for assigning a value to a variable. The most common assignment operator is = 

Operator

Example

Same as

=a = ba = b
+ =a += ba = a+b
- =a -= ba = a-b
* =a *= ba = a*b
/ =a /= ba = a/b
% =a %= ba = a%b


Example 3: Assignment Operators


Output




4. C Relational Operators :-


A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making(If-else) and loops. 

Operator

Meaning of Operators

Example

==Equal to5 == 3 is evaluated to 0
>Greater than5 > 3 is evaluated to 1
<Less than5 < 3 is evaluated to 0
!=Not equal to5 != 3 is evaluated to 1
>=Greater than or equal to5 >= 3 is evaluated to 1
<=Less than or equal to5 <= 3 is evaluated to 0

No comments:

Post a Comment

Please don't comment any spam link in comment.