SlideShare a Scribd company logo
Operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Misc Operators
Arithmetic Operators
Assume variable Aholds 10 and variable B holds 20 then:
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an
integer division
B % A will give 0
++ Increments operator increases integer value
by one
A++ will give 11
-- Decrements operator decreases integer
value by one
A-- will give 9
#include <stdio.h>
void main()
{
int a=20;
int b=10;
printf("Answer %d+%d=%d n",a,b,a+b);
printf("Answer %d-%d=%d n",a,b,a-b);
printf("Answer %d*%d=%d n",a,b,a*b);
printf("Answer %d/%d=%d n",a,b,a/b);
printf("Answer %d MODULAS %d=%d n",a,b,a%b);
printf("Answer a++=%d n",a,a++);
printf("Answer a--=%d n",a,a--);
}
Answer 20+10=30
Answer 20-10=10
Answer 20*10=200
Answer 20/10=2
Answer 20 MODULAS 10=0
Answer a++=21
Answer a--=20
Press any key to continue . . .
output
Relational Operators
Assume variable Aholds 10 and variable B holds 20, then:
Operator Description Example
== Checks if the values of two operands are equal or
not, if yes then condition becomes true.
(A == B) is not true.
!= Checks if the values of two operands are equal or
not, if values are not equal then condition becomes
true.
(A != B) is true.
> Checks if the value of left operand is greater than the
value of right operand, if yes then condition becomes
true.
(A > B) is not true.
< Checks if the value of left operand is less than the
value of right operand, if yes then condition becomes
true.
(A < B) is true.
>= Checks if the value of left operand is greater than or
equal to the value of right operand, if yes then
condition becomes true.
(A >= B) is not true.
<= Checks if the value of left operand is less than or
equal to the value of right operand, if yes then
condition becomes true.
(A <= B) is true.
#include <stdio.h>
void main()
{
int a=20;
int b=10;
int c;
c=(a==b);
c=(a!=b);
c=(a<b);
c=(a>b);
c=(a<=b);
c=(a<=b);
printf("Answer %d==%d= %d n",a,b,c);
printf("Answer %d!=%d= %d n",a,b,c);
printf("Answer %d<%d= %d n",a,b,c);
printf("Answer %d>%d= %d n",a,b,c);
printf("Answer %d<=%d= %d n",a,b,c);
printf("Answer %d>=%d= %d n",a,b,c);
}
Answer 20==10= 0
Answer 20!=10= 0
Answer 20<10= 0
Answer 20>10= 0
Answer 20<=10= 0
Answer 20>=10= 0
Press any key to continue . . .
OUTPUT
#include <stdio.h>
void main()
{
int a=20;
int b=10;
int c;
c=(a==b);
printf("Answer %d==%d= %d n",a,b,c);
c=(a!=b);
printf("Answer %d!=%d= %d n",a,b,c);
c=(a<b);
printf("Answer %d<%d= %d n",a,b,c);
c=(a>b);
printf("Answer %d>%d= %d n",a,b,c);
c=(a<=b);
printf("Answer %d<=%d= %d n",a,b,c);
c=(a<=b);
printf("Answer %d>=%d= %d n",a,b,c);
}
Answer 20==10= 0
Answer 20!=10= 1
Answer 20<10= 0
Answer 20>10= 1
Answer 20<=10= 0
Answer 20>=10= 0
Press any key to continue
. . .
OUTPUT
#include <stdio.h>
void main()
{
int a=20;
int b=10;
printf("Answer %d==%d= %d n",a,b,a==b);
printf("Answer %d!=%d= %d n",a,b,a!=b);
printf("Answer %d<%d= %d n",a,b,a<b);
printf("Answer %d>%d= %d n",a,b,a>b);
printf("Answer %d<=%d= %d n",a,b,a<=b);
printf("Answer %d>=%d= %d n",a,b,a<=b);
}
Answer 20==10= 0
Answer 20!=10= 1
Answer 20<10= 0
Answer 20>10= 1
Answer 20<=10= 0
Answer 20>=10= 0
Press any key to continue . . .
Logical Operators
Assume variable A holds 1 and variable B holds 0, then:
Operator Description Example
&& Called Logical AND operator. If both the
operands are non-zero, then condition
becomes true.
(A && B) is false.
|| Called Logical OR Operator. If any of the
two operands is non-zero, then condition
becomes true
(A || B) is true.
! Called Logical NOT Operator. Use to
reverses the logical state of its operand. If
a condition is true then Logical NOT
operator will make false.
!(A && B) is true.
// LOGICAL AND
#include <stdio.h>
void main()
{
printf("______________n");
printf("| A B C |n");
printf("______________n");
printf("| 0 0 %d |n",0&&0);
printf("| 0 1 %d |n",0&&1);
printf("| 1 0 %d |n",1&&0);
printf("| 1 1 %d |n",1&&1);
printf("______________n");
}
______________
| A B C |
______________
| 0 0 0 |
| 0 1 0 |
| 1 0 0 |
| 1 1 1 |
______________
Press any key to continue . . .
OUTPUT
// LOGICAL OR
#include <stdio.h>
void main()
{
printf("______________n");
printf("| A B C |n");
printf("______________n");
printf("| 0 0 %d |n",0||0);
printf("| 0 1 %d |n",0||1);
printf("| 1 0 %d |n",1||0);
printf("| 1 1 %d |n",1||1);
printf("______________n");
}
______________
| A B C |
______________
| 0 0 0 |
| 0 1 1 |
| 1 0 1 |
| 1 1 1 |
______________
Press any key to continue . . .
OUTPUT
// LOGICAL NOT GATE
#include <stdio.h>
void main()
{
int a=0;
int b=1;
printf("__________________________n");
printf("| A(input) C(output) |n");
printf("|________________________|n");
printf("| 0 %d |n",!a);
printf("| 1 %d |n",!b);
printf("|________________________|n");
}
OUTPUT
__________________________
| A(input) C(output) |
|________________________|
| 0 1 |
| 1 0 |
|________________________|
Press any key to continue . . .
// LOGICAL NOT+OR=NOR
#include <stdio.h>
void main()
{
printf("______________n");
printf("| A B C |n");
printf("______________n");
printf("| 0 0 %d |n",!(0||0));
printf("| 0 1 %d |n",!(0||1));
printf("| 1 0 %d |n",!(1||0));
printf("| 1 1 %d |n",!(1||1));
printf("______________n");
}
______________
| A B C |
______________
| 0 0 1 |
| 0 1 0 |
| 1 0 0 |
| 1 1 0 |
______________
Press any key to continue . . .
// LOGICAL NOT+AND=NAND
#include <stdio.h>
void main()
{
printf("______________n");
printf("| A B C |n");
printf("______________n");
printf("| 0 0 %d |n",!(0&&0));
printf("| 0 1 %d |n",!(0&&1));
printf("| 1 0 %d |n",!(1&&0));
printf("| 1 1 %d |n",!(1&&1));
printf("______________n");
}
______________
| A B C |
______________
| 0 0 1 |
| 0 1 1 |
| 1 0 1 |
| 1 1 0 |
______________
Press any key to continue . . .
OUTPUT
Operator Description Example
& Binary AND Operator copies a bit to the result if it
exists in both operands.
(A & B) will give 12, which is
0000 1100
| Binary OR Operator copies a bit if it exists in either
operand.
(A | B) will give 61, which is
0011 1101
^ Binary XOR Operator copies the bit if it is set in
one operand but not both.
(A ^ B) will give 49, which is
0011 0001
~ Binary Ones Complement Operator is unary and
has the effect of 'flipping' bits.
(~A ) will give -61, which is
1100 0011 in 2's complement
form.
<< Binary Left Shift Operator. The left operands value
is moved left by the number of bits specified by
the right operand.
A << 2 will give 240 which is
1111 0000
>> Binary Right Shift Operator. The left operands
value is moved right by the number of bits
specified by the right operand.
A >> 2 will give 15 which is
0000 1111
Bitwise Operators
// BITWISE AND
#include <stdio.h>
void main()
{
int a=0;
int b=1;
printf("__________________________n");
printf("| A B C(output) |n");
printf("|________________________|n");
printf("| 0 0 %d |n",0&0);
printf("| 0 1 %d |n",0&1);
printf("| 1 0 %d |n",1&0);
printf("| 1 1 %d |n",1&1);
printf("|________________________|n");
}
__________________________
| A B C(output) |
|________________________|
| 0 0 0 |
| 0 1 0 |
| 1 0 0 |
| 1 1 1 |
|________________________|
Press any key to continue . . .
// BINARY OR
#include <stdio.h>
void main()
{
int a=0;
int b=1;
printf("__________________________n");
printf("| A B C(output) |n");
printf("|________________________|n");
printf("| 0 0 %d |n",0|0);
printf("| 0 1 %d |n",0|1);
printf("| 1 0 %d |n",1|0);
printf("| 1 1 %d |n",1|1);
printf("|________________________|n");
}
__________________________
| A B C(output) |
|________________________|
| 0 0 0 |
| 0 1 1 |
| 1 0 1 |
| 1 1 1 |
|________________________|
Press any key to continue . . .
// BINARY XOR
#include <stdio.h>
void main()
{
int a=0;
int b=1;
printf("__________________________n");
printf("| A B C(output) |n");
printf("|________________________|n");
printf("| 0 0 %d |n",0^0);
printf("| 0 1 %d |n",0^1);
printf("| 1 0 %d |n",1^0);
printf("| 1 1 %d |n",1^1);
printf("|________________________|n");
}
__________________________
| A B C(output) |
|________________________|
| 0 0 0 |
| 0 1 1 |
| 1 0 1 |
| 1 1 0 |
|________________________|
Press any key to continue . . .
// BINARY COMPLEMNT
#include <stdio.h>
void main()
{
int a=0;
int b=1;
printf("__________________________n");
printf("| A B C(output) |n");
printf("|________________________|n");
printf("| 0 0 %d |n",~(60));
printf("|________________________|n");
}
__________________________
| A B C(output) |
|________________________|
| 0 0 -61 |
|________________________|
Press any key to continue . . .
Bitwise operator works on bits and perform bit-by-bit operation.
The truth tables for &, |, and ^ are as follows:
p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume if A = 60; and B = 13; now in binary format they will be as
follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Assignment Operators
Operator Description Example
= Simple assignment operator, Assigns values from right side operands to
left side operand
C = A + B will assign value of A + B into
C
+= Add AND assignment operator, It adds right operand to the left operand
and assign the result to left operand
C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the
left operand and assign the result to left operand
C -= A is equivalent to C = C - A
*= Multiply AND assignment operator, It multiplies right operand with the
left operand and assign the result to left operand
C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right
operand and assign the result to left operand
C /= A is equivalent to C = C / A
%= Modulus AND assignment operator, It takes modulus using two
operands and assign the result to left operand
C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2
Misc Operators ↦ sizeof & ternary
Operator Description Example
sizeof() Returns the size of an variable. sizeof(a), where a is
integer, will return 4.
& Returns the address of an variable. &a; will give actual
address of the variable.
* Pointer to a variable. *a; will pointer to a
variable.
? : Conditional Expression If Condition is true ? Then
value X : Otherwise value
Y

More Related Content

PPTX
C Programming Language Step by Step Part 5
PPTX
C Programming Language Part 8
PPTX
C Programming Language Part 7
PPTX
C Programming Language Part 9
PPTX
C Programming Language Part 11
PPTX
Expressions using operator in c
PPTX
Decision making and branching
PDF
Let us c(by yashwant kanetkar) chapter 2 solution
C Programming Language Step by Step Part 5
C Programming Language Part 8
C Programming Language Part 7
C Programming Language Part 9
C Programming Language Part 11
Expressions using operator in c
Decision making and branching
Let us c(by yashwant kanetkar) chapter 2 solution

What's hot (19)

DOC
Basic c programs updated on 31.8.2020
PDF
Understanding storage class using nm
PPTX
Function basics
DOC
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
PPTX
How c program execute in c program
PDF
88 c-programs
DOCX
Practical write a c program to reverse a given number
PDF
C lab programs
PDF
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
DOCX
Practical write a c program to reverse a given number
DOCX
Let us C (by yashvant Kanetkar) chapter 3 Solution
DOCX
Core programming in c
PDF
Introduction to Computer and Programing - Lecture 04
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
Basic c programs updated on 31.8.2020
Understanding storage class using nm
Function basics
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
How c program execute in c program
88 c-programs
Practical write a c program to reverse a given number
C lab programs
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Practical write a c program to reverse a given number
Let us C (by yashvant Kanetkar) chapter 3 Solution
Core programming in c
Introduction to Computer and Programing - Lecture 04
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
Ad

Viewers also liked (19)

PPTX
C Programming Language Part 5
PPTX
C Programming Language Step by Step Part 1
PPTX
C Programming Language Part 6
PPTX
Basic c programming and explanation PPT1
PPTX
My first program in c, hello world !
PPT
Steps for Developing a 'C' program
PPTX
Programming in C Basics
PDF
Composicio Digital _Practica Pa4
PPTX
Introduction to Basic C programming 02
PPSX
C programming basics
DOC
у рідному краї серце співає
DOCX
Eric Stewart Resume
PPTX
media slide
DOC
реквієм за убієнних голодом
PDF
Work Life Leader Manifesto
PDF
20141203 第5回社内勉強会(佐藤)
PPT
відповідаючи перед майбутнім
PPTX
ME461 Final Presentation
PPTX
Ashida Project Division
C Programming Language Part 5
C Programming Language Step by Step Part 1
C Programming Language Part 6
Basic c programming and explanation PPT1
My first program in c, hello world !
Steps for Developing a 'C' program
Programming in C Basics
Composicio Digital _Practica Pa4
Introduction to Basic C programming 02
C programming basics
у рідному краї серце співає
Eric Stewart Resume
media slide
реквієм за убієнних голодом
Work Life Leader Manifesto
20141203 第5回社内勉強会(佐藤)
відповідаючи перед майбутнім
ME461 Final Presentation
Ashida Project Division
Ad

Similar to C Programming Language Part 4 (20)

PPTX
C operators
PPTX
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
PPTX
C Operators and Control Structures.pptx
PPTX
COM1407: C Operators
PPTX
operators_group_2[1].pptx PLEASE DOWNLOAD
DOCX
C – operators and expressions
PPTX
PROGRAMMING IN C - Operators.pptx
PDF
C Operators and Control Structures.pdf
PDF
ICP - Lecture 5
PPTX
C - programming - Ankit Kumar Singh
PPTX
Operators
PDF
C programming Assignments and Questions.pdf
PPTX
C operators
PDF
Programming C Part 02
PDF
Python : basic operators
PPTX
Operators in C programming language.pptx
PPTX
btwggggggggggggggggggggggggggggggisop correct (1).pptx
PDF
Programming for Problem Solving
DOCX
Qust & ans inc
C operators
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
C Operators and Control Structures.pptx
COM1407: C Operators
operators_group_2[1].pptx PLEASE DOWNLOAD
C – operators and expressions
PROGRAMMING IN C - Operators.pptx
C Operators and Control Structures.pdf
ICP - Lecture 5
C - programming - Ankit Kumar Singh
Operators
C programming Assignments and Questions.pdf
C operators
Programming C Part 02
Python : basic operators
Operators in C programming language.pptx
btwggggggggggggggggggggggggggggggisop correct (1).pptx
Programming for Problem Solving
Qust & ans inc

More from Rumman Ansari (19)

PDF
Sql tutorial
PDF
C programming exercises and solutions
PDF
Java Tutorial best website
DOCX
Java Questions and Answers
DOCX
servlet programming
PPTX
C program to write c program without using main function
PPTX
Steps for c program execution
PPTX
Pointer in c program
PPTX
What is token c programming
PPTX
What is identifier c programming
PPTX
What is keyword in c programming
PPTX
Type casting in c programming
PPTX
C Programming Language Step by Step Part 3
PPTX
C Programming Language Step by Step Part 2
DOCX
C Programming
PPTX
Tail recursion
PPTX
Tail Recursion in data structure
PDF
Spyware manual
PPTX
Linked list
Sql tutorial
C programming exercises and solutions
Java Tutorial best website
Java Questions and Answers
servlet programming
C program to write c program without using main function
Steps for c program execution
Pointer in c program
What is token c programming
What is identifier c programming
What is keyword in c programming
Type casting in c programming
C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 2
C Programming
Tail recursion
Tail Recursion in data structure
Spyware manual
Linked list

Recently uploaded (20)

PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PPT
Chapter 6 Design in software Engineeing.ppt
PPTX
Road Safety tips for School Kids by a k maurya.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Practice Questions on recent development part 1.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Drone Technology Electronics components_1
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
Sustainable Sites - Green Building Construction
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Internship_Presentation_Final engineering.pptx
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Queuing formulas to evaluate throughputs and servers
PPTX
Internet of Things (IOT) - A guide to understanding
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
Chapter 6 Design in software Engineeing.ppt
Road Safety tips for School Kids by a k maurya.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Practice Questions on recent development part 1.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Drone Technology Electronics components_1
ETO & MEO Certificate of Competency Questions and Answers
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Sustainable Sites - Green Building Construction
Operating System & Kernel Study Guide-1 - converted.pdf
Geodesy 1.pptx...............................................
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Internship_Presentation_Final engineering.pptx
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Queuing formulas to evaluate throughputs and servers
Internet of Things (IOT) - A guide to understanding

C Programming Language Part 4

  • 1. Operators 1. Arithmetic Operators 2. Relational Operators 3. Logical Operators 4. Bitwise Operators 5. Assignment Operators 6. Misc Operators
  • 2. Arithmetic Operators Assume variable Aholds 10 and variable B holds 20 then: Operator Description Example + Adds two operands A + B will give 30 - Subtracts second operand from the first A - B will give -10 * Multiplies both operands A * B will give 200 / Divides numerator by de-numerator B / A will give 2 % Modulus Operator and remainder of after an integer division B % A will give 0 ++ Increments operator increases integer value by one A++ will give 11 -- Decrements operator decreases integer value by one A-- will give 9
  • 3. #include <stdio.h> void main() { int a=20; int b=10; printf("Answer %d+%d=%d n",a,b,a+b); printf("Answer %d-%d=%d n",a,b,a-b); printf("Answer %d*%d=%d n",a,b,a*b); printf("Answer %d/%d=%d n",a,b,a/b); printf("Answer %d MODULAS %d=%d n",a,b,a%b); printf("Answer a++=%d n",a,a++); printf("Answer a--=%d n",a,a--); } Answer 20+10=30 Answer 20-10=10 Answer 20*10=200 Answer 20/10=2 Answer 20 MODULAS 10=0 Answer a++=21 Answer a--=20 Press any key to continue . . . output
  • 4. Relational Operators Assume variable Aholds 10 and variable B holds 20, then: Operator Description Example == Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true. != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.
  • 5. #include <stdio.h> void main() { int a=20; int b=10; int c; c=(a==b); c=(a!=b); c=(a<b); c=(a>b); c=(a<=b); c=(a<=b); printf("Answer %d==%d= %d n",a,b,c); printf("Answer %d!=%d= %d n",a,b,c); printf("Answer %d<%d= %d n",a,b,c); printf("Answer %d>%d= %d n",a,b,c); printf("Answer %d<=%d= %d n",a,b,c); printf("Answer %d>=%d= %d n",a,b,c); } Answer 20==10= 0 Answer 20!=10= 0 Answer 20<10= 0 Answer 20>10= 0 Answer 20<=10= 0 Answer 20>=10= 0 Press any key to continue . . . OUTPUT
  • 6. #include <stdio.h> void main() { int a=20; int b=10; int c; c=(a==b); printf("Answer %d==%d= %d n",a,b,c); c=(a!=b); printf("Answer %d!=%d= %d n",a,b,c); c=(a<b); printf("Answer %d<%d= %d n",a,b,c); c=(a>b); printf("Answer %d>%d= %d n",a,b,c); c=(a<=b); printf("Answer %d<=%d= %d n",a,b,c); c=(a<=b); printf("Answer %d>=%d= %d n",a,b,c); } Answer 20==10= 0 Answer 20!=10= 1 Answer 20<10= 0 Answer 20>10= 1 Answer 20<=10= 0 Answer 20>=10= 0 Press any key to continue . . . OUTPUT
  • 7. #include <stdio.h> void main() { int a=20; int b=10; printf("Answer %d==%d= %d n",a,b,a==b); printf("Answer %d!=%d= %d n",a,b,a!=b); printf("Answer %d<%d= %d n",a,b,a<b); printf("Answer %d>%d= %d n",a,b,a>b); printf("Answer %d<=%d= %d n",a,b,a<=b); printf("Answer %d>=%d= %d n",a,b,a<=b); } Answer 20==10= 0 Answer 20!=10= 1 Answer 20<10= 0 Answer 20>10= 1 Answer 20<=10= 0 Answer 20>=10= 0 Press any key to continue . . .
  • 8. Logical Operators Assume variable A holds 1 and variable B holds 0, then: Operator Description Example && Called Logical AND operator. If both the operands are non-zero, then condition becomes true. (A && B) is false. || Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true (A || B) is true. ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.
  • 9. // LOGICAL AND #include <stdio.h> void main() { printf("______________n"); printf("| A B C |n"); printf("______________n"); printf("| 0 0 %d |n",0&&0); printf("| 0 1 %d |n",0&&1); printf("| 1 0 %d |n",1&&0); printf("| 1 1 %d |n",1&&1); printf("______________n"); } ______________ | A B C | ______________ | 0 0 0 | | 0 1 0 | | 1 0 0 | | 1 1 1 | ______________ Press any key to continue . . . OUTPUT
  • 10. // LOGICAL OR #include <stdio.h> void main() { printf("______________n"); printf("| A B C |n"); printf("______________n"); printf("| 0 0 %d |n",0||0); printf("| 0 1 %d |n",0||1); printf("| 1 0 %d |n",1||0); printf("| 1 1 %d |n",1||1); printf("______________n"); } ______________ | A B C | ______________ | 0 0 0 | | 0 1 1 | | 1 0 1 | | 1 1 1 | ______________ Press any key to continue . . . OUTPUT
  • 11. // LOGICAL NOT GATE #include <stdio.h> void main() { int a=0; int b=1; printf("__________________________n"); printf("| A(input) C(output) |n"); printf("|________________________|n"); printf("| 0 %d |n",!a); printf("| 1 %d |n",!b); printf("|________________________|n"); } OUTPUT __________________________ | A(input) C(output) | |________________________| | 0 1 | | 1 0 | |________________________| Press any key to continue . . .
  • 12. // LOGICAL NOT+OR=NOR #include <stdio.h> void main() { printf("______________n"); printf("| A B C |n"); printf("______________n"); printf("| 0 0 %d |n",!(0||0)); printf("| 0 1 %d |n",!(0||1)); printf("| 1 0 %d |n",!(1||0)); printf("| 1 1 %d |n",!(1||1)); printf("______________n"); } ______________ | A B C | ______________ | 0 0 1 | | 0 1 0 | | 1 0 0 | | 1 1 0 | ______________ Press any key to continue . . .
  • 13. // LOGICAL NOT+AND=NAND #include <stdio.h> void main() { printf("______________n"); printf("| A B C |n"); printf("______________n"); printf("| 0 0 %d |n",!(0&&0)); printf("| 0 1 %d |n",!(0&&1)); printf("| 1 0 %d |n",!(1&&0)); printf("| 1 1 %d |n",!(1&&1)); printf("______________n"); } ______________ | A B C | ______________ | 0 0 1 | | 0 1 1 | | 1 0 1 | | 1 1 0 | ______________ Press any key to continue . . . OUTPUT
  • 14. Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12, which is 0000 1100 | Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61, which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49, which is 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -61, which is 1100 0011 in 2's complement form. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 0000 1111 Bitwise Operators
  • 15. // BITWISE AND #include <stdio.h> void main() { int a=0; int b=1; printf("__________________________n"); printf("| A B C(output) |n"); printf("|________________________|n"); printf("| 0 0 %d |n",0&0); printf("| 0 1 %d |n",0&1); printf("| 1 0 %d |n",1&0); printf("| 1 1 %d |n",1&1); printf("|________________________|n"); } __________________________ | A B C(output) | |________________________| | 0 0 0 | | 0 1 0 | | 1 0 0 | | 1 1 1 | |________________________| Press any key to continue . . .
  • 16. // BINARY OR #include <stdio.h> void main() { int a=0; int b=1; printf("__________________________n"); printf("| A B C(output) |n"); printf("|________________________|n"); printf("| 0 0 %d |n",0|0); printf("| 0 1 %d |n",0|1); printf("| 1 0 %d |n",1|0); printf("| 1 1 %d |n",1|1); printf("|________________________|n"); } __________________________ | A B C(output) | |________________________| | 0 0 0 | | 0 1 1 | | 1 0 1 | | 1 1 1 | |________________________| Press any key to continue . . .
  • 17. // BINARY XOR #include <stdio.h> void main() { int a=0; int b=1; printf("__________________________n"); printf("| A B C(output) |n"); printf("|________________________|n"); printf("| 0 0 %d |n",0^0); printf("| 0 1 %d |n",0^1); printf("| 1 0 %d |n",1^0); printf("| 1 1 %d |n",1^1); printf("|________________________|n"); } __________________________ | A B C(output) | |________________________| | 0 0 0 | | 0 1 1 | | 1 0 1 | | 1 1 0 | |________________________| Press any key to continue . . .
  • 18. // BINARY COMPLEMNT #include <stdio.h> void main() { int a=0; int b=1; printf("__________________________n"); printf("| A B C(output) |n"); printf("|________________________|n"); printf("| 0 0 %d |n",~(60)); printf("|________________________|n"); } __________________________ | A B C(output) | |________________________| | 0 0 -61 | |________________________| Press any key to continue . . .
  • 19. Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ are as follows: p q p & q p | q p ^ q 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1
  • 20. Assume if A = 60; and B = 13; now in binary format they will be as follows: A = 0011 1100 B = 0000 1101 ----------------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011
  • 21. Assignment Operators Operator Description Example = Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A <<= Left shift AND assignment operator C <<= 2 is same as C = C << 2 >>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2 &= Bitwise AND assignment operator C &= 2 is same as C = C & 2 ^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2 |= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2
  • 22. Misc Operators ↦ sizeof & ternary Operator Description Example sizeof() Returns the size of an variable. sizeof(a), where a is integer, will return 4. & Returns the address of an variable. &a; will give actual address of the variable. * Pointer to a variable. *a; will pointer to a variable. ? : Conditional Expression If Condition is true ? Then value X : Otherwise value Y