Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

C Programming Operators

The document provides an overview of operators in C programming, including arithmetic, increment/decrement, assignment, relational, logical, and bitwise operators. It explains how these operators function through examples and code snippets, detailing their usage in expressions and decision-making. Additionally, it covers the concept of expressions, including arithmetic, relational, logical, and conditional expressions.

Uploaded by

rajoanatahosin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

C Programming Operators

The document provides an overview of operators in C programming, including arithmetic, increment/decrement, assignment, relational, logical, and bitwise operators. It explains how these operators function through examples and code snippets, detailing their usage in expressions and decision-making. Additionally, it covers the concept of expressions, including arithmetic, relational, logical, and conditional expressions.

Uploaded by

rajoanatahosin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

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).

Operator Meaning of Operator


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

// Working of arithmetic operators


#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
C Arithmetic Operators
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
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
operators, meaning they only operate on a single operand.

// Working of increment and decrement operators


#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
C Increment and Decrement Operators
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000

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 =

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
C Assignment Operators
Example 3: Assignment Operators

// Working of assignment operators


#include <stdio.h>
int main()
{
int a = 5, c;

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.

Relational operators are used in decision making and loops.

perator Meaning of Operator 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


C Relational Operators
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

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.

Operator Meaning Example

Logical AND. True only if all If c = 5 and d = 2 then, expression


&&
operands are true ((c==5) && (d>5)) equals to 0.

Logical OR. True only if either one If c = 5 and d = 2 then, expression


||
operand is true ((c==5) || (d>5)) equals to 1.

Logical NOT. True only if the If c = 5 then, expression !(c==5)


!
operand is 0 equals to 0.
C Logical Operators
// Working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);

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

(a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).


(a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
(a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
(a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
!(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
!(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).
C Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are
converted to bit-level which makes processing faster and saves power.
Bitwise operators are used in C programming to perform bit-level operations.

Operators Meaning of operators

& Bitwise AND


| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
Other Operators

Comma Operator
Comma operators are used to link related expressions together. For example:

int a, c = 5, d;

The sizeof operator


The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).

#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

An arithmetic expression is an expression that consists of operands and


arithmetic operators. An arithmetic expression computes a value of type int,
float or double.

When an expression contains only integral operands, then it is known as pure


integer expression when it contains only real operands, it is known as pure real
expression, and when it contains both integral and real operands, it is known as
mixed mode expression.

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".

a>=9 It is used to check whether the value of a is greater than or equal to 9.


Relational Expressions
1.#include <stdio.h>
2. int main()
3.{
4.
5. int x=4;
6. if(x%2==0)
7. {
8. printf("The number x is even");
9. }
10. else
11. printf("The number x is not even");
12. return 0;
13.}
Logical Expressions
•A logical expression is an expression that computes either a zero or non-zero value.
•It is a complex test condition to take a decision.
Logical Expressions Description
( x > 4 ) && ( x < 6 ) It is a test condition to check whether the x is greater than 4 and x is less
than 6. The result of the condition is true only when both the conditions
are true.

x > 10 || y <11 It is a test condition used to check whether x is greater than 10 or y is


less than 11. The result of the test condition is true if either of the
conditions holds true value.

! ( 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.}

You might also like