C Programming Operators
C Programming Operators
Outline
Operator
Operator types
Expressions
C Programming 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.
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on
numerical values (constants and variables).
Here, the operators ++ and -- are used as prefixes. These two operators can also be used as postfixes like a++ and a--. Visit
this page to learn more about how increment and decrement operators work when used as postfix.
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
= 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
C Assignment Operators
Example 3: Assignment Operators
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
C Assignment Operators
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
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.
== Equal to 5 == 3 is evaluated to 0
return 0;
}
C Relational Operators
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression
results true or false. Logical operators are commonly used in decision making in C programming.
return 0;
}
C Logical Operators
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Explanation of logical operator program
Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
C Expressions
An expression is a formula in which operands are linked to each other by the use of operators to compute
a value. An operand can be a function reference, a variable, an array element or a constant.
Let's see an example:
1.a-b;
In the above expression, minus character (-) is an operator, and a, and b are the two operands.
There are four types of expressions exist in C:
•Arithmetic expressions
•Relational expressions
•Logical expressions
•Conditional expressions
Each type of expression takes certain types of operands and uses a specific set of operators. Evaluation of a particular
expression produces a specific value.
For example:
1.x = 9/2 + a-b;
The entire above line is a statement, not an expression. The portion after the equal is an expression.
Arithmetic Expressions
Example
6*2/ (2+1 * 2/3 + 6) + 8 * (8/4)
Relational Expressions
•A relational expression is an expression used to compare two operands.
•It is a condition which is used to decide whether the action should be taken or not.
•In relational expressions, a numeric value cannot be compared with the string value.
•The result of the relational expression can be either zero or non-zero value. Here, the zero value is
equivalent to a false and non-zero value is equivalent to true.
Relational Expression Description
x%2 = = 0 This condition is used to check whether the x is an even number or not. The
relational expression results in value 1 if x is an even number otherwise results in
value 0.
a!=b It is used to check whether a is not equal to b. This relational expression results in
1 if a is not equal to b otherwise 0.
a+b = = x+y It is used to check whether the expression "a+b" is equal to the expression "x+y".
! ( x > 10 ) && ( y = = 2 ) It is a test condition used to check whether x is not greater than 10 and y
is equal to 2. The result of the condition is true if both the conditions are
true.
Logical Expressions
1.#include <stdio.h>
2.int main()
3.{
4. int x = 4;
5. int y = 10;
6. if ( (x <10) && (y>5))
7. {
8. printf("Condition is true");
9. }
10. else
11. printf("Condition is false");
12. return 0;
13.}
Conditional Expressions
•A conditional expression is an expression that returns 1 if the condition is true
otherwise 0.
•A conditional operator is also known as a ternary operator.
The Syntax of Conditional operator
Suppose exp1, exp2 and exp3 are three expressions.
exp1 ? exp2 : exp3
The above expression is a conditional expression which is evaluated on the basis
of the value of the exp1 expression. If the condition of the expression exp1 holds
true, then the final conditional expression is represented by exp2 otherwise
represented by exp3.
Conditional Expressions
1.#include<stdio.h>
2.#include<string.h>
3.int main()
4.{
5. int age = 25;
6. char status;
7. status = (age>22) ? 'M': 'U';
8. if(status == 'M')
9. printf("Married");
10. else
11. printf("Unmarried");
12. return 0;
13.}