0% found this document useful (0 votes)
16 views125 pages

Unit 1 - Operator (Part 3)

The document provides an overview of various types of operators in C programming, including unary, arithmetic, relational, bitwise, logical, ternary, assignment, and comma operators. It explains the functionality and syntax of increment and decrement operators, as well as arithmetic operations and their precedence rules. Additionally, it covers relational operators and their usage in decision-making and loops.

Uploaded by

piushu88
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)
16 views125 pages

Unit 1 - Operator (Part 3)

The document provides an overview of various types of operators in C programming, including unary, arithmetic, relational, bitwise, logical, ternary, assignment, and comma operators. It explains the functionality and syntax of increment and decrement operators, as well as arithmetic operations and their precedence rules. Additionally, it covers relational operators and their usage in decision-making and loops.

Uploaded by

piushu88
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

Operator

Operator
• An operator is simply a symbol that is used to perform operations.
There can be many types of operations like arithmetic, logical,
bitwise, etc.
• There are following types of operators to perform different types of
operations in C language.
 Unary operator
 Arithmetic Operators
 Relational Operators
 Bitwise Operator
 Logical Operators
 Ternary or Conditional Operators
 Assignment Operator
 Comma Operator
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %

+ - Left to right
Bitwise Shift Operators >> << Left to right

Relational Operators < <= > >= Left to right

== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right

||(Logical OR) Left to right

Ternary or Conditional ?: Right to left


Operators

Assignment Operator = += -= *= /= %= >>= <<= &= Right to left


^= |=

Comma Operator , Left to right


Points to note:
• Bitwise not(~) is a Unary operator
• Logical not(!) is a Unary operator
Unary operator
• Unary operator are operators that act upon a
single operand to produce a new value.
• Types of unary operators:
 unary minus(-)
 increment(++)
 decrement(- -)
 NOT(!)
 Address of operator(&)
 sizeof()
unary minus
• The minus operator changes the sign of its
argument. A positive number becomes
negative, and a negative number becomes
positive.
int a = 10;
int b = -a; // b = -10
• unary minus is different from subtraction
operator, as subtraction requires two
operands.
increment
• It is used to increment the value of the variable by 1. The increment
can be done in two ways:
• prefix increment
In this method, the operator precedes the operand (e.g., ++a). The
value of operand will be altered before it is used.
int a = 1;
int b = ++a; // b = 2
• postfix increment
In this method, the operator follows the operand (e.g., a++). The
value operand will be altered after it is used.
int a = 1;
int b = a++; // b = 1
int c = a; // c = 2
decrement
• It is used to decrement the value of the variable by 1. The
decrement can be done in two ways:
• prefix decrement
In this method, the operator precedes the operand (e.g., – -a). The
value of operand will be altered before it is used.
int a = 1;
int b = --a; // b = 0
• postfix decrement
In this method, the operator follows the operand (e.g., a- -). The
value of operand will be altered after it is used.
int a = 1;
int b = a--; // b = 1
int c = a; // c = 0
NOT(!)
• It is used to reverse the logical state of its
operand. If a condition is true, then Logical
NOT operator will make it false. If x is true,
then !x is false If x is false, then !x is true
Addressof operator(&)
• It gives an address of a variable. It is used to
return the memory address of a variable.
These addresses returned by the address-of
operator are known as pointers because they
“point” to the variable in memory.& gives an
address on variable n
int a;
int *ptr;
ptr = &a; // address of a is copied to the location ptr.
sizeof()
• This operator returns the size of its operand, in bytes. The sizeof operator always precedes its
operand. The operand is an expression, or it may be a cast.

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;
}
Output:
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Points to Remember
• Pre increment have highest priority.
• Post increment have lowest priority than all
the operator even from Assignment operator.
C Programming

Increment operators
Increment Operators:

Increment Operators are used to increase the value of the


variable by one.

Increment operators are used on a single operand or


variable, That’s why, it is called unary operator.

* The priority of unary operators are higher than the other


operators.

C Programming
Syntax:

++ // increment operator

Examples:
a++;
++a;

* x= 4++; // gives error, because 4 is constant


*y= ++5; // gives error, because 5 is constant

C Programming
Type of Increment Operators

1. Pre Increment

2. Post Increment

C Programming
Pre Increment Operator (++variable):

Syntax:

++ variable;

C Programming
Pre Increment Operator:

b = ++a;

a=a+1;
b=a;

C Programming
void main()
{
int a,b;
a=3;
clrscr();
b=++a;
printf(“%d%d”,a,b);
getch();
}

C Programming
Post Increment Operator (variable++):

Syntax:

variable++;

C Programming
Post Increment Operator:

b = a++;

b=a;
a=a+1;

C Programming
void main()
{
int a,b;
a=3;
clrscr();
b=a++;
printf(“%d%d”,a,b);
getch();
}

C Programming
C Programming

Arithmetic Operators
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %

+ - Left to right
Bitwise Shift Operators >> << Left to right

Relational Operators < <= > >= Left to right

== != Left to right
Bitwise Operator &() Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right

||(Logical OR) Left to right

Ternary or Conditional Operators ?: Right to left

Assignment Operator = += -= *= /= %= >>= <<= &= ^= |= Right to left

Comma Operator , Bitwise ANDLeft to right


1. Addition +
2. Subtraction -
3. Multiplication *
4. Division /
5. Modulus %

C Programming
Precedence Rule for Arithmetic Operators
Precedence Rule for Arithmetic Operators:

1 stPriority: *, /, %
nd
2 Priority: +, -

C Programming
Addition (+):

operand1+operand2;

Example:
S=10+20;

C Programming
Subtraction (-):

operand1- operand2;

Example:
S=20-10;

C Programming
Multiplication (*):

operand1 * operand2;

Example:
M=5*10;

C Programming
Division (/):

operand1 / operand2;

Example:
10/2; answer: 5
7/3 answer: 2
3/7 answer: 0

C Programming
Operation Result

int/int = int
int/float = float
float/int = float
float/float = float

C Programming
Modulus Operator(%):

operand1 % operand2;

Example:
a=10%2;

C Programming
Arithmetic Instructions
Arithmetic Instructions:

Arithmetic instructions are used to perform arithmetic


operations between constants and variables.

C Programming
n= 2*3/4+5-6%3;

C Programming
n= 2*3/4+5-6%3;
n=6/4+5-6%3; // 2*3
n=1+5-6%3; // 6/4
n=1+5-0; // 6%3
n=6-0; // 1+5
n=6;

C Programming
C Programming

Relational operators
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %

+ - Left to right
Bitwise Shift Operators >> << Left to right

Relational Operators < <= > >= Left to right

== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right

||(Logical OR) Left to right

Ternary or Conditional Operators ?: Right to left

Assignment Operator = += -= *= /= %= >>= <<= &= ^= |= Right to left

Comma Operator , Left to right


Relational Operators:

A relational operator checks the relationship between two


operands.

If relation is true, then it returns 1 and if the relation is false,


therefore, it returns value 0.

Relational operators are used in decision making and


loops.

C Programming
Type of Relational Operators
1. Less than ( < )
2. Less than or equal to ( <= )
3. Greater than ( > )
4. Greater than or equal to ( >=)
5. Not equal to ( != )
6. Comparison Operator ( == )

C Programming
Precedence Rule for Relational Operators
Precedence Rule for Relational Operators:

1 st Priority: < <= > >=

2 nd Priority: == !=

C Programming
Less than ( < ):

if x<y then this expression will be true.

C Programming
Less than ( < ):

void main()
{
int a,b,c;
a=4<5; // value of a = 1
b=14<5; // value of b = 0
c=5<5; // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Less than ( < ):

void main()
{
int a,b,c,x;
x=4;
a=x<5; // value of a = 1
b=5<x; // value of b = 0
c=x<4; // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Less than or equal to ( <= ):

if x<=y then this expression will be true.

C Programming
Less than or equal to ( <= ):

void main()
{
int a,b,c;
a=(4<=5); // value of a = 1
b=(14<=5); // value of b = 0
c=(5<=5); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Less than or equal to ( <= ):

void main()
{
int a,b,c,x;
x=4;
a=(x<=5); // value of a = 1
b=(5<=x); // value of b = 0
c=(x<=4); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Greater than ( > ):

if x>y then this expression will be true.

C Programming
Greater than ( > ):

void main()
{
int a,b,c;
a= 4>5; // value of a = 0
b= 14>5; // value of b = 1
c= 5>5; // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Greater than ( > ):

void main()
{
int a,b,c,x;
x=4;
a=x>5; // value of a = 0
b=5>x; // value of b = 1
c=x>4; // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Greater than or equal to( >= ):

if x>=y then this expression will be true.

C Programming
Greater than or equal to( >= ):

void main()
{
int a,b,c;
a= (4>=5); // value of a = 0
b= (14>=5); // value of b = 1
c= (5>=5); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Greater than or equal to( >= ):

void main()
{
int a,b,c,x;
x=4;
a=(x>=5); // value of a = 0
b=(5>=x); // value of b = 1
c=(x>=4); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Comparison Operator or double equals to ( == ):

if x==y then this expression will be true.

C Programming
Comparison Operator or double equals to ( == ):

void main()
{
int a, b, c;
a= (4==5); // value of a = 0
b= (14==5); // value of b = 0
c= (5==5); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Comparison Operator or double equals to ( == ):

void main()
{
int a,b,c,x;
x=4;
a=(x==5); // value of a = 0
b=(5==x); // value of b = 0
c=(x==4); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Not equal to ( != ):

if x!=y then this expression will be true.

C Programming
Not equal to ( != ):

void main()
{
int a, b, c;
a= (4!=5); // value of a = 1
b= (14!=5); // value of b = 1
c= (5!=5); // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Not equal to ( != ):

void main()
{
int a,b,c,x;
x=4;
a=(x!=5); // value of a = 1
b=(5!=x); // value of b = 1
c=(x!=4); // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Example: Example: Example:

void main() void main()


void main()
{ {
{
int x; int x;
int x;
x=3<=4; x=5>4>3;
x=3>4;
printf(“%d”,x); printf(“%d”,x);
printf(“%d”,x);
getch(); getch();
getch();
} }
}

Output:
Output: 0 Output: 1
C Programming
Bitwise Operator in C
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %

+ - Left to right
Bitwise Shift Operators >> << Left to right

Relational Operators < <= > >= Left to right

== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right

||(Logical OR) Left to right

Ternary or Conditional ?: Right to left


Operators

Assignment Operator = += -= *= /= %= >>= <<= &= Right to left


^= |=

Comma Operator , Left to right


Bitwise Operator
• The bitwise operators are the operators used
to perform the operations on the data at the
bit-level.
• When we perform the bitwise operations,
then it is also known as bit-level programming.
It consists of two digits, either 0 or 1.
• It is mainly used in numerical computations to
make the calculations faster.
We have different types of bitwise operators in the C
programming language. The following is the list of the
bitwise operators:

Operator Meaning of operator

& Bitwise AND operator

| Bitwise OR operator

^ Bitwise exclusive OR operator

One's complement operator (unary


~
operator)

<< Left shift operator

>> Right shift operator


Truth table of the bitwise operators.

X Y X&Y X|Y X^Y

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0
Bitwise AND operator

• Bitwise AND operator is denoted by the single ampersand sign (&). Two
integer operands are written on both sides of the (&) operator.
• If the corresponding bits of both the operands are 1, then the output of
the bitwise AND operation is 1; otherwise, the output would be 0.
For example,
• We have two variables a and b.
a =6;
b=4;
The binary representation of the above two variables are given below:
a = 0110
b = 0100
When we apply the bitwise AND operation in the above two variables, i.e.,
a&b, the output would be:
Result = 0100
Program using Bitwise AND operator

#include <stdio.h>
int main()
{
int a=6, b=14; // variable declarations
printf("The output of the Bitwise AND operator a&b is %d",a&b);
return 0;
}

a AND b = 0110 && 1110 = 0110


Output:
Bitwise OR operator

• The bitwise OR operator is represented by a single vertical sign (|).


Two integer operands are written on both sides of the (|) symbol. If
the bit value of any of the operand is 1, then the output would be 1,
otherwise 0.
For example,
• We consider two variables,
a = 23;
b = 10;
The binary representation of the above two variables would be:
a = 0001 0111
b = 0000 1010
When we apply the bitwise OR operator in the above two variables,
i.e., a|b , then the output would be:
Result = 0001 1111
Program using Bitwise OR operator

#include <stdio.h>
int main()
{
int a=23,b=10; // variable declarations
printf("The output of the Bitwise OR operator a|b is %d",a|b);
return 0;
}

a = 0001 0111 and b = 0000 1010


OUTPUT:
Bitwise exclusive OR operator

• Bitwise exclusive OR operator is denoted by (^) symbol. Two


operands are written on both sides of the exclusive OR operator. If
the corresponding bit of any of the operand is 1 then the output
would be 1, otherwise 0.
For example,
• We consider two variables a and b,
a = 12;
b = 10;
The binary representation of the above two variables would be:
a = 0000 1100
b = 0000 1010
When we apply the bitwise exclusive OR operator in the above two
variables (a^b), then the result would be:
Result = 0000 0110
Program using Bitwise exclusive OR
operator
#include <stdio.h>
int main()
{
int a=12,b=10; // variable declarations
printf("The output of the Bitwise exclusive OR operator a^b is %d",a^b);
return 0;
}

a = 0000 1100 and b = 0000 1010


Output:
Bitwise complement operator

• The bitwise complement operator is also known as one's


complement operator. It is represented by the symbol tilde (~). It
takes only one operand or variable and performs complement
operation on an operand. When we apply the complement
operation on any bits, then 0 becomes 1 and 1 becomes 0.

For example,
• If we have a variable named 'a',
a = 8;
The binary representation of the above variable is given below:
a = 1000
When we apply the bitwise complement operator to the operand, t
hen the output would be:
Result = 0111
Program using Bitwise complement
operator
#include <stdio.h>
int main()
{
int a=8; // variable declarations
printf("The output of the Bitwise complement operator ~a is %d",~a);
return 0;
}

• Output
Bitwise shift operators

• Two types of bitwise shift operators exist in C


programming. The bitwise shift operators will
shift the bits either on the left-side or right-
side. Therefore, we can say that the bitwise
shift operator is divided into two categories:
• Left-shift operator
• Right-shift operator
Left-shift operator

• It is an operator that shifts the number of bits to the left-side.

• Syntax of the left-shift operator is given below:


Operand << n

Where,
Operand is an integer expression on which we apply the left-shift
operation.
n is the number of bits to be shifted.

• In the case of Left-shift operator, 'n' bits will be shifted on the left-side.
The 'n' bits on the left side will be popped out, and 'n' bits on the right-
side are filled with 0.
For example,

• Suppose we have a statement:


int a = 5;
The binary representation of 'a' is given below:
a = 0101
If we want to left-
shift the above representation by 2, then the stat
ement would be:
a << 2;
0101<<2 = 010100
Program using Left-shift operator

#include <stdio.h>
int main()
{
int a=5; // variable initialization
printf("The value of a<<2 is : %d ", a<<2);
return 0;
}

Output:
Right-shift operator

• It is an operator that shifts the number of bits to the right


side.
• Syntax of the right-shift operator is given below:
Operand >> n;

Where,
• Operand is an integer expression on which we apply the
right-shift operation.
• N is the number of bits to be shifted.
• In the case of the right-shift operator, 'n' bits will be shifted
on the right-side. The 'n' bits on the right-side will be
popped out, and 'n' bits on the left-side are filled with 0.
For example,

Suppose we have a statement,


int a = 7;
The binary representation of the above variable would be:
a = 0111
If we want to right
shift the above representation by 2, then the statment would be:
a>>2;
0000 0111 >> 2 = 0000 0001
Program using Right-shift operator
#include <stdio.h>
int main()
{
int a=7; // variable initialization
printf("The value of a>>2 is : %d ", a>>2);
return 0;
}

Output:
Logical Operator
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %

+ - Left to right
Bitwise Shift Operators >> << Left to right

Relational Operators < <= > >= Left to right

== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right

||(Logical OR) Left to right

Ternary or Conditional Operators ?: Right to left

Assignment Operator = += -= *= /= %= >>= <<= &= ^= |= Right to left

Comma Operator , Left to right


C Programming

Logical Operator
The Logical operators are used to perform logical operations
on the given expressions.

Logical Operators are used with binary variables.

They are mainly used in conditional statements and loops for


evaluating a condition.

C Programming
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.

Logical operators are used to combine and evaluate two or


more conditions.

C Programming
Types of Logical Operators:

1. Logical AND (&&)


2. Logical OR (||)
3. Logical NOT (!)

C Programming
Logical AND (&&):

A B A&&B
0 0 0
1 0 0
0 1 0
1 1 1

C Programming
Logical OR (||):

A B A||B
0 0 0
1 0 1
0 1 1
1 1 1

C Programming
Logical NOT (!):

A !A
0 1
1 0

C Programming
C Programming

Logical Instructions
The Logical operators are used to perform logical operations
on the given expressions.

These expressions are known as logical instructions in C


Programming.

C Programming
void main()
{
int a;
clrscr();
a=2||3&&4||0&&5||!7&&6;
printf(“%d”,a);
getch();
}

C Programming
Precedence Rule for Logical Operators:

1. Logical NOT ( ! )
2. Logical AND ( && )
3. Logical OR ( || )

C Programming
void main()
{
int a;
clrscr();
a=2||3&&4||0&&5||!7&&6;
printf(“%d”,a);
getch();
}

C Programming
a=2 || 3&&4 || 0&&5 || !7&&6;
a=2 || 3&&4 || 0&&5 || 0 &&6; // !7 = 0
a=2 || 1 || 0&&5 || 0 &&6; // 3&&4 = 1
a=2 || 1 || 0 || 0 &&6; // 0&&5 = 0
a=2 || 1 || 0 || 0 // 0&&6 = 0
a=1 || 0 || 0 // 2||1 = 1
a=1 || 0 // 1 || 0 = 1
a=1 // 1 || 0 = 1

C Programming
Example: Example: Example:

void main() void main() void main()


{ { {
int y,x=5; int y,x=5; int y,x=5;
clrscr(); clrscr(); clrscr();
y=!x>4 y=x>4 && x<10 y=x<4|| x<10
printf(“%d”,y); printf(“%d”,y); printf(“%d”,y);
getch(); getch(); getch();
} } }

OUTPUT: 0 OUTPUT: 1 OUTPUT: 1


Conditional Operator in C
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %

+ - Left to right
Bitwise Shift Operators >> << Left to right

Relational Operators < <= > >= Left to right

== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right

||(Logical OR) Left to right

Ternary or Conditional ?: Right to left


Operators

Assignment Operator = += -= *= /= %= >>= <<= &= Right to left


^= |=

Comma Operator , Left to right


Conditional Operator in C

• The conditional operator is also known as a


ternary operator. The conditional statements are
the decision-making statements which depends
upon the output of the expression. It is
represented by two symbols, i.e., '?' and ':'.
• As conditional operator works on three operands,
so it is also known as the ternary operator.
• The behavior of the conditional operator is
similar to the 'if-else' statement as 'if-else'
statement is also a decision-making statement.
Syntax of a conditional operator
Expression1? expression2: expression3;

Meaning of the above syntax.


In the above syntax, the expression1 is a Boolean condition that can be either true or false
value.
If the expression1 results into a true value, then the expression2 will execute.
The expression2 is said to be true only when it returns a non-zero value.
If the expression1 returns false value then the expression3 will execute.
The expression3 is said to be false only when it returns zero value.
Example 1 of Conditional Operator
#include <stdio.h>
int main()
{
int age; // variable declaration
printf("Enter your age");
scanf("%d",&age); // taking user input for age variable
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting")); // conditional operator
return 0;
}
Explanation of example 1
• In the given code, we are taking input as the
'age' of the user. After taking input, we have
applied the condition by using a conditional
operator. In this condition, we are checking
the age of the user. If the age of the user is
greater than or equal to 18, then the
statement1 will execute, i.e., (printf("eligible
for voting")) otherwise, statement2 will
execute, i.e., (printf("not eligible for voting")).
Output of example 1
Example 2 of Conditional Operator
#include <stdio.h>
int main()
{
int a=5,b; // variable declaration
b=((a==5)?(3):(2)); // conditional operator
printf("The value of 'b' variable is : %d",b);
return 0;
}
Explanation of example 2
• In the above code, we have declared two
variables, i.e., 'a' and 'b', and assign 5 value to
the 'a' variable. After the declaration, we are
assigning value to the 'b' variable by using the
conditional operator. If the value of 'a' is equal
to 5 then 'b' is assigned with a 3 value
otherwise 2.
Output of example 2
Assignment Operator

Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a
variable and right side operand of the assignment operator is a value. The value on the right side must be of the same
data-type of the variable on the left side otherwise the compiler will raise an error.
Different types of assignment operators are shown below:

• “=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable
on the left.
For example:

a = 10; b = 20;
ch = 'y';

• “+=”. This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the
variable on left to the value on the right and then assigns the result to the variable on the left.
Example:

(a += b) can be written as (a = a + b)

If initially value stored in a is 5. Then (a += 6) = 11.

• “-=”:This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the
variable on left from the value on the right and then assigns the result to the variable on the left. Example:
• (a -= b) can be written as (a = a - b)

If initially value stored in a is 8. Then (a -= 6) = 2.

• “*=”This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the
variable on left to the value on the right and then assigns the result to the variable on the left. Example:

(a *= b) can be written as (a = a * b)

If initially value stored in a is 5. Then (a *= 6) = 30.

• “/=”This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the
variable on left by the value on the right and then assigns the result to the variable on the left.
Example:

(a /= b) can be written as (a = a / b)

If initially value stored in a is 6. Then (a /= 2) = 3.


Comma Operator
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %

+ - Left to right
Bitwise Shift Operators >> << Left to right

Relational Operators < <= > >= Left to right

== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right

||(Logical OR) Left to right

Ternary or Conditional ?: Right to left


Operators

Assignment Operator = += -= *= /= %= >>= <<= &= Right to left


^= |=

Comma Operator , Left to right


Comma as Separator
• In C programming language, comma (,) works as a
separator and an operator too and its behaviour is
little different according to the place where it is used.
1) Comma (,) as separator
While declaration multiple variables and providing
multiple arguments in a function, comma works as a
separator.
Example:
• int a,b,c;
• In this statement, comma is a separator and tells to
the compiler that these (a, b, and c) are three
different variables.
Comma as operator
2) Comma (,) as an operator
Sometimes we assign multiple values to a variable using comma, in that
case comma is known as operator.
Example:
• a = 10,20,30;
• b = (10,20,30);

In the first statement, value of a will be 10, because assignment operator


(=) has more priority more than comma (,), thus 10 will be assigned to the
variable a.

In the second statement, value of b will be 30, because 10, 20, 30 are
enclosed in braces, and braces has more priority than assignment (=)
operator. When multiple values are given with comma operator within the
braces, then right most value is considered as result of the expression.
Thus, 30 will be assigned to the variable b.
Example:

#include <stdio.h>
int main()
{
int a,b; Output
a = 10,20,30; a= 10, b= 30
b = (10,20,30);
//printing the values
printf("a= %d, b= %d\n",a,b);
return 0;
}
NOTE
• Try not to confuse between comma as a separator and
comma as an operator. Sample example:
int a = 4, 3;
• This will generate an error as comma in this case acts as a
separator as declaration takes place. So the error less code
will be as follows:
int a;
a = 4,3;
Now the value stored in a will be 4.
• Also, the following is valid,
int a =(4, 3);
here , 3 is stored in a.
Arrow operator
> An Arrow operator in C allows to access elements in Structures and Unions.
> It is used with a pointer variable pointing to a structure or union.
> The arrow operator is formed by using a minus sign, followed by the greater than
symbol as shown below.

Syntax:

(pointer_name)->(variable_name)

Operation: The -> operator in C or C++ gives the value held by variable_name to structure or
union variable pointer_name.

Difference between Dot(.) and Arrow(->) operator:

> The Dot(.) operator is used to normally access members of a structure or union.
> The Arrow(->) operator exists to access the members of the structure or the unions using
pointers.
Not equal to Relational Operator ( != )
Constant term cannot be placed on left hand side.

Example: 1=x; is invalid.

C Programming
Constant term can be placed in the left hand side.

Example: 1==1 is valid and returns 1.

C Programming
C Programming

Assignment Operator
vs
Comparison Operator
Assignment Operator:

The assignment operator is used to assign the value to the


variable.

For example:

int a = 10;
float b = 20.50;
char ch = 'y';
10 = a
C Programming
Comparison Operator:

The comparison operator checks whether the two given


operands are equal or not. If so, it returns one. Otherwise it
returns zero.

For example:

3==3 is valid and returns 1.

C Programming

You might also like