0% found this document useful (0 votes)
5 views

C Programming Part 4

VARDHAMAN COLLEGE OF ENGINEERING - C Programming

Uploaded by

molocof324
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C Programming Part 4

VARDHAMAN COLLEGE OF ENGINEERING - C Programming

Uploaded by

molocof324
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

OPERATORS AND EXPRESSIONS:

Operators : An operator is a Symbol that performs an operation. An operators acts some


variables are called operands to get the desired result.

Ex : a+b;
Where a,b are operands and + is the operator.

Types of Operator :
1) Arithmetic Operators.
2) Relational Operators.
3) Logical Operators.
4) Assignment Operators.
5). Unary Operators.
6) Conditional Operators.
7) Special Operators.
8) Bitwise Operators.
9) Shift Operators.

Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction and
multiplication on numerical values (constants and variables).
C Program to demonstrate the working of arithmetic operators
#include <stdio.h>
void 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);

C PROGRAMMING Page 31
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

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.
Operands may be variables, constants or expressions.

Relational operators are used in decision making and loops.

Operator Meaning Example Return value


< is less than 2<9 1
<= is less than or equal to 2<=2 1
> is greater than 2>9 0
>= is greater than or equal to 3>=2 1
== is equal to 2==3 0
!= is not equal to 2!=2 0

// C Program to demonstrate the working of relational operators

#include <stdio.h>

int main()

int a = 5, b = 5, c = 10;

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

printf("%d == %d = %d \n", a, c, a == c); // false

printf("%d > %d = %d \n", a, b, a > b); //false

printf("%d > %d = %d \n", a, c, a > c); //false

C PROGRAMMING Page 32
printf("%d < %d = %d \n", a, b, a < b); //false

printf("%d < %d = %d \n", a, c, a < c); //true

printf("%d != %d = %d \n", a, b, a != b); //false

printf("%d != %d = %d \n", a, c, a != c); //true

printf("%d >= %d = %d \n", a, b, a >= b); //true

printf("%d >= %d = %d \n", a, c, a >= c); //false

printf("%d <= %d = %d \n", a, b, a <= b); //true

printf("%d <= %d = %d \n", a, c, a <= c); //true

return 0;

Output

5 == 5 = 1

5 == 10 = 0

5>5=0

5 > 10 = 0

5<5=0

5 < 10 = 1

5 != 5 = 0

5 != 10 = 1

5 >= 5 = 1

5 >= 10 = 0

C PROGRAMMING Page 33
5 <= 5 = 1

5 <= 10 = 1

Logical Operators.

These operators are used to combine the results of two or more conditions. 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 Return value


&& Logical AND (9>2)&&(17>2) 1
|| Logical OR (9>2) || (17 = = 7) 1
! Logical NOT 29!=29 0
Logical AND : If any one condition false the complete condition becomes false.

Truth Table
Op1 Op2 Op1 && Op2
true true true
true false false
false true false
false false false

Logical OR : If any one condition true the complete condition becomes true.

Truth Table
Op1 Op2 Op1 // Op2
true true true
true false true
false true true
false false false

Logical Not : This operator reverses the value of the expression it operates on i.e, it makes a
true expression false and false expression true.

Op1 Op1 !
true false
false true

// C Program to demonstrate the working of logical operators

#include <stdio.h>

C PROGRAMMING Page 34
int main()

int a = 5, b = 5, c = 10, result;

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

printf("(a = b) && (c > b) equals to %d \n", result);

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

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

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

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

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

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

result = !(a != b);

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

result = !(a == b);

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

return 0;

Output

(a = b) && (c > b) equals to 1

(a = b) && (c < b) equals to 0

(a = b) || (c < b) equals to 1

C PROGRAMMING Page 35
(a != b) || (c < b) equals to 0

!(a != b) equals to 1

!(a == b) equals to 0

Assignment Operators. Assignment operators are used to assign a value (or) an expression
(or) a value of a variable to another variable.

Syntax : variable name=expression (or) value (or) variable

Ex : x=10;
y=a+b;
z=p;

Compound assignment operator:

„C‟ provides compound assignment operators to assign a value to variable in order to assign a
new value to a variable after performing a specified operation.

Operator Example Meaning


+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y X=x%y

// C Program to demonstrate the working of assignment operators

#include <stdio.h>

int main()

int a = 5, c;

c = a;

C PROGRAMMING Page 36
printf("c = %d \n", c);

c += a; // c = c+a

printf("c = %d \n", c);

c -= a; // c = c-a

printf("c = %d \n", c);

c *= a; // c = c*a

printf("c = %d \n", c);

c /= a; // c = c/a

printf("c = %d \n", c);

c %= a; // c = c%a

printf("c = %d \n", c);

return 0;

Output

c=5

c = 10

c=5

c = 25

c=5

c=0

Increment and Decrement Operators /Unary Operators:

C PROGRAMMING Page 37
Unary operators are having higher priority than the other operators. Unary operators, meaning
they only operate on a single operand.

Increment Operator in C Programming

1. Increment operator is used to increment the current value of variable by adding integer 1.

2. Increment operator can be applied to only variables.


3. Increment operator is denoted by ++.

We have two types of increment operator i.e Pre-Increment and Post-Increment Operator.

Pre-Increment

Pre-increment operator is used to increment the value of variable before using in the expression. In
the Pre-Increment value is first incremented and then used inside the expression.

b = ++y;

In this example suppose the value of variable „y‟ is 5 then value of variable „b‟ will be 6 because
the value of „y‟ gets modified before using it in a expression.

Post-Increment

Post-increment operator is used to increment the value of variable as soon as after executing
expression completely in which post increment is used. In the Post-Increment value is first used
in a expression and then incremented.

b = x++;

In this example suppose the value of variable „x‟ is 5 then value of variable „b‟ will be 5 because
old value of „x‟ is used.

Note :

We cannot use increment operator on the constant values because increment operator operates on
only variables. It increments the value of the variable by 1 and stores the incremented value back
to the variable

C PROGRAMMING Page 38
b = ++5;

or

b = 5++;

The syntax of the operators is given below.


++<variable name> --<variable name>
<variable name>++ <variable name>--
The operator ++ adds 1 to the operand and – subtracts 1 from the operand. These operators in
two forms : prefix (++x) and postfix(x++).

Operator Meaning
++x Pre increment
- -x Pre decrement
x++ Post increment
x-- Post decrement

Where
1 : ++x : Pre increment, first increment and then do the operation.
2 : - -x : Pre decrement, first decrements and then do the operation.
3 : x++ : Post increment, first do the operation and then increment.
4 : x- - : Post decrement, first do the operation and then decrement.

// C Program to demonstrate the 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;
}

Output
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000

C PROGRAMMING Page 39
Multiple increment operators inside printf
#include<stdio.h>
void main() {
int i = 1;
printf("%d %d %d", i, ++i, i++);
}
Output : 3 3 1

Pictorial representation

Explanation of program

I am sure you will get confused after viewing the above image and output of program.
1. Whenever more than one format specifiers (i.e %d) are directly or indirectly related with
same variable (i,i++,++i) then we need to evaluate each individual expression from right
to left.
2. As shown in the above image evaluation sequence of expressions written inside printf
will be – i++,++i,i
3. After execution we need to replace the output of expression at appropriate place
No Step Explanation

Evaluate
1 At the time of execution we will be using older value of i = 1
i++

Evaluate At the time of execution we will be increment value already modified after
2
++i step 1 i.e i = 3

2 Evaluate i At the time of execution we will be using value of i modified in step 2

C PROGRAMMING Page 40

You might also like