Operators in C
Operators in C
One of the most important features of C is that it has a very rich set of built in operators including arithmetic, relational, logical, and bitwise operators.
An operator can be any symbol like + - * / that specifies what operation need to be performed on the data.
For ex: + indicates addition operation* indicates multiplication operation
An operand can be a constant or a variable.
An expression is combination of operands and operators that reduces to a single value.
For ex: Consider the following expression a + b here a and b are operands, while + is an operator.
Types of C operators
C language offers many types of operators.
- Arithmetic operators.
- Assignment operators.
- Relational operators.
- Logical operators.
- Bit wise operators.
- Conditional operators (ternary operators).
- Increment/decrement operators.
- Special operators.
An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables).
|%||Modulus (Remainder )|
Assignment Operators in C
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
It assign the value from right to left side variable.
|=||Simple Equal||C = A + B will assign the value of A + B to C|
|+=||Add & assignment||C += A is equivalent to C = C + A|
|-=||Subtract & assignment||C -= A is equivalent to C = C - A|
|*=||Multiply & assignment||C *= A is equivalent to C = C * A|
|/=||Divide & assignment||C /= A is equivalent to C = C / A|
|%=||Modulus & assignment||C %= A is equivalent to C = C % A|
Relational Operators in C
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 and loops.
|>||greater than |
|<||less than |
|>=||greater than or equal to |
|<=||less than or equal to |
|==||equal to |
|!=||not equal to |
Logical Operators in C
Logical operators are used to combine two or more condition for building complex expression.
|&&||AND||Logical AND. True only if all operands are true|
|||||OR||Logical OR. True only if either one operand is true|
|!||NOT||Logical NOT. True only if the operand is 0|
Increment and Decrement Operators in C
Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.
Type of Increment Operator
- Pre increment/decrement Operator (++a, --a)
- Post increment/decrement Operator (a++, a--)
Conditional Operator in C
The conditional operator (? :) is a ternary operator. This operator needs 3 operands to perform the operation.
Variable = <test expression> ? <expression 1> : <expression 2>
<test expression> ? <expression 1> : <expression 2>
The conditional operator works as follows:
Test Expression is Condition
Expression 1 is Statement Followed if Condition is True
Expression 2 is Statement Followed if Condition is False
max=(num1>num2)?num1:num2; printf("Max is %d",max);
The output is:
max is 20
(num%2==0)?printf("Number is Even"):printf("Number is Odd");
The Output is:
Number is Odd
C : Operators in C Program List
Write a program to input number of days from user and display it, week and days format.
input is : 20 days
then output is:
2 week and 6 days.
Write a program to input number of days from user and display it, Month, Week and Days format. (consider 30days = 1 month)
input is : 50 days
output is: 1 month, 2 week and 6 days.