➕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.
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.0, b = 2.0, c = 5 and d = 2. Then in C programming, 2. C Increment and Decrement Operators:-
C 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. C 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 = b | a = b |
| + = | a += b | a = a+b |
| - = | a -= b | a = a-b |
| * = | a *= b | a = a*b |
| / = | a /= b | a = a/b |
| % = | a %= b | a = 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 to | 5 == 3 is evaluated to 0 |
| > | Greater than | 5 > 3 is evaluated to 1 |
| < | Less than | 5 < 3 is evaluated to 0 |
| != | Not equal to | 5 != 3 is evaluated to 1 |
| >= | Greater than or equal to | 5 >= 3 is evaluated to 1 |
| <= | Less than or equal to | 5 <= 3 is evaluated to 0 |

No comments:
Post a Comment
Please don't comment any spam link in comment.