SlideShare a Scribd company logo
Introduction to Programming (ECS 102)
Instructors:
Dr. Jasabanta Patro and Dr. Rini Smita Thakur
Operators and Expressions
Expression
• Constants
• Variables
• Array Elements
• Function References
Joined by
operators
Expression
Operands: numeric values
• integer quantities,
• floating-point quantities
• or characters (remember that
character constants represent
integer values, as determined by the
computer’s character set).
● Division and Modulus operator: second operand is non-zero
introduction to c programming - Topic 3.pdf
Modulus Operator Examples
x = 3;
y = 4;
// using modulo operator
result = x % y;
printf("%d", result); Ans:3 (x % y) = x ……… if (x < y)
result = y % x;
printf("n%d", result); Ans: 1
x = 4;
y = 2;
result = x % y;
printf("n%d", result); Ans:0
Restrictions on the Modulus Operator
● % modulus operator cannot be applied to floating-point numbers i.e. float or
double.
Modulus Operation with Negative Operand
Here for a % b, the
sign of left operand is
appended to the result.
Ans: -3
Most versions of C will determine the
sign of the remainder in this manner,
though this feature is unspecified in the
formal definition of the language.
introduction to c programming - Topic 3.pdf
Suppose that i is an integer variable whose value is 7, and f
is a floating-point variable whose value is 8.5.
The expression
(i + f) % 4 is invalid, because the first operand (i + f ) is
floating-point rather than integer.
((int) (i + f)) % 4
forces the first operand to be an integer and is therefore
valid, resulting in the integer remainder 3.
((int) (i + f)) % 4
forces the first operand to be an integer and is therefore
valid, resulting in the integer remainder 3.
Note that the explicit type specification applies only to the
first operand, not the entire expression.
EXAMPLE 3.6 Suppose that f is a floating-point variable whose value is 5.5. The
expression
((int) f) % 2
contains two integer operands and is therefore valid, resulting in the integer
remainder 1.
Note, however, that f remains a floating-point variable whose value is 5.5, even
though the value off was converted to an integer (5) when carrying out
the remainder operation.
Precedence of Operators
● The operators within C are grouped hierarchically according to their
precedence (i.e., order of evaluation).
● Operations with a higher precedence are carried out before operations having
a lower precedence.
● Precedence Level:
○ Brackets
○ Division/Multiplication/ Modulus (Left to right associativity)
○ Addition/Subtraction (Left to Right Associativity)
Precedence Level:
• Brackets
• Division/Multiplication
Modulus
• Addition/Subtraction
Precedence Level:
• Brackets
• Division/Multiplication/
Modulus
• Addition/Subtraction
introduction to c programming - Topic 3.pdf
Unary Operators
● C includes a class of operators that act upon a single operand to produce a
new value.
● Types of unary operators
1. Unary minus ( – )
2. Increment ( ++ )
3. Decrement ( — )
4. NOT ( ! )
5. Addressof operator ( & )
6. sizeof()
1. 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
2. Increment
The increment operator ( ++ )
is used to increment the value of the variable by 1.
The increment can be done in two ways:
2.1 prefix increment
In this method, the operator precedes the operand (e.g., ++a).
The value of the operand will be altered before it is used.
Example:
int a = 1;
int b = ++a; // b = 2
2.2 postfix increment
In this method, the operator follows the operand (e.g., a++).
The value operand will be altered after it is used.
Example:
int a = 1;
int b = a++; // b = 1
int c = a; // c = 2
● Prefix Increment (initial value of i is 1)
● Output
• Postfix Increment
3. Decrement
● The decrement operator ( — ) 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
the operand will be altered before it is used.
Example:
int a = 1;
int b = --a; // b = 0
● postfix decrement
● In this method, the operator follows the operand (e.g., a- -). The value of the
operand will be altered after it is used.
Example:
int a = 1;
int b = a--; // b = 1
int c = a; // c = 0
4. NOT ( ! )
The logical NOT operator ( ! ) is used to reverse the
logical state of its operand. If a condition is true, then
the Logical NOT operator will make it false.
Example:
If x is true, then !x is false
If x is false, then !x is true
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.
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
Relational and Logical Operators
introduction to c programming - Topic 3.pdf
Relational and equality operators always
assign the value 0 or 1.
Condition is true= 1
Condition is false=0
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
Assignment Operators
● There are several different assignment operators in C.
● It assign the value of an expression to an identifier.
● The most commonly used assignment operator is =. Assignment expressions
that make use of this operator are written in the form
● identifier = expression
● where identifier generally represents a variable, and expression represents a
constant, a variable or a more complex expression.
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
Conditional Operators
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
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
Find the output x?
x= y = =z
The crux of the question lies in the statement x = y==z.
The operator == is executed before = because
precedence of comparison operators (<=, >= and ==) is
higher than assignment operator =.
The result of a comparison operator is either 0 or 1
based on the comparison result.
int x, y = 5, z = 5;
introduction to c programming - Topic 3.pdf
Let us consider the condition inside the if statement.
Since there are two greater than (>) operators in the
expression “c > b > a”, associativity of > is considered.
Associativity of > is left to right. So, expression c > b >
a is evaluated as ( (c > b) > a ). And since the (c > b) is
being the relational operator it will return 1 if True
otherwise 0 is if False.
introduction to c programming - Topic 3.pdf
If value of a,b,and c =50, 10, 20 then find the value of above expression.
C Library Functions
C Library Functions
C Library Functions
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
Chapter ends here

More Related Content

PPTX
C Operators and Control Structures.pptx
PDF
C Operators and Control Structures.pdf
PDF
Unit ii chapter 1 operator and expressions in c
PPTX
3. C_OperatorsExpressions on c languyage.pptx
PPT
cprogrammingoperator.ppt
PPTX
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
PPT
C operator and expression
PPT
class noteson operetors in c programming .ppt
C Operators and Control Structures.pptx
C Operators and Control Structures.pdf
Unit ii chapter 1 operator and expressions in c
3. C_OperatorsExpressions on c languyage.pptx
cprogrammingoperator.ppt
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
C operator and expression
class noteson operetors in c programming .ppt

Similar to introduction to c programming - Topic 3.pdf (20)

PDF
Types of Operators in C
PPTX
Operators and expressions in C++
PPTX
Operators and expressions in c language
PPTX
Operators inc c language
PPTX
PROGRAMMING IN C - Operators.pptx
PPTX
Operators-computer programming and utilzation
PPTX
Operators in C Programming
PPT
Cprogrammingoperator
PPTX
OPERATORS OF C++
PPTX
C operators
PPTX
COM1407: C Operators
PPTX
This slide contains information about Operators in C.pptx
PPTX
C operators
PDF
C++ Expressions Notes
PPTX
C Operators
PPTX
Operators in C programming language.pptx
PPTX
Operators1.pptx
PPTX
c programming2.pptx
PDF
Module2.1_Programming_Branching_and_looping.pdf
PPTX
Operators in c by anupam
Types of Operators in C
Operators and expressions in C++
Operators and expressions in c language
Operators inc c language
PROGRAMMING IN C - Operators.pptx
Operators-computer programming and utilzation
Operators in C Programming
Cprogrammingoperator
OPERATORS OF C++
C operators
COM1407: C Operators
This slide contains information about Operators in C.pptx
C operators
C++ Expressions Notes
C Operators
Operators in C programming language.pptx
Operators1.pptx
c programming2.pptx
Module2.1_Programming_Branching_and_looping.pdf
Operators in c by anupam
Ad

More from rajd20284 (8)

PDF
signals and systems lec 13 modulation.pdf
PDF
signals and systems lec 12 filtering.pdf
PDF
C programming Assignments and Questions.pdf
PDF
BIO102 : Cell Biology lecture slides.pdf
PDF
introduction to c programming : Topic 5.pdf
PDF
introduction to c programming : Topic 4.pdf
PDF
introduction to c programming - Topic 2.pdf
PDF
introduction to c programming - Topic 1.pdf
signals and systems lec 13 modulation.pdf
signals and systems lec 12 filtering.pdf
C programming Assignments and Questions.pdf
BIO102 : Cell Biology lecture slides.pdf
introduction to c programming : Topic 5.pdf
introduction to c programming : Topic 4.pdf
introduction to c programming - Topic 2.pdf
introduction to c programming - Topic 1.pdf
Ad

Recently uploaded (20)

PDF
PPT on Performance Review to get promotions
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPT
Project quality management in manufacturing
PPT
Mechanical Engineering MATERIALS Selection
PDF
composite construction of structures.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
DOCX
573137875-Attendance-Management-System-original
PPT on Performance Review to get promotions
Fundamentals of safety and accident prevention -final (1).pptx
Geodesy 1.pptx...............................................
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Embodied AI: Ushering in the Next Era of Intelligent Systems
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Model Code of Practice - Construction Work - 21102022 .pdf
Project quality management in manufacturing
Mechanical Engineering MATERIALS Selection
composite construction of structures.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
III.4.1.2_The_Space_Environment.p pdffdf
Operating System & Kernel Study Guide-1 - converted.pdf
Current and future trends in Computer Vision.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
R24 SURVEYING LAB MANUAL for civil enggi
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
573137875-Attendance-Management-System-original

introduction to c programming - Topic 3.pdf

  • 1. Introduction to Programming (ECS 102) Instructors: Dr. Jasabanta Patro and Dr. Rini Smita Thakur
  • 3. Expression • Constants • Variables • Array Elements • Function References Joined by operators Expression
  • 4. Operands: numeric values • integer quantities, • floating-point quantities • or characters (remember that character constants represent integer values, as determined by the computer’s character set).
  • 5. ● Division and Modulus operator: second operand is non-zero
  • 7. Modulus Operator Examples x = 3; y = 4; // using modulo operator result = x % y; printf("%d", result); Ans:3 (x % y) = x ……… if (x < y) result = y % x; printf("n%d", result); Ans: 1
  • 8. x = 4; y = 2; result = x % y; printf("n%d", result); Ans:0
  • 9. Restrictions on the Modulus Operator ● % modulus operator cannot be applied to floating-point numbers i.e. float or double.
  • 10. Modulus Operation with Negative Operand Here for a % b, the sign of left operand is appended to the result.
  • 11. Ans: -3 Most versions of C will determine the sign of the remainder in this manner, though this feature is unspecified in the formal definition of the language.
  • 13. Suppose that i is an integer variable whose value is 7, and f is a floating-point variable whose value is 8.5. The expression (i + f) % 4 is invalid, because the first operand (i + f ) is floating-point rather than integer. ((int) (i + f)) % 4 forces the first operand to be an integer and is therefore valid, resulting in the integer remainder 3.
  • 14. ((int) (i + f)) % 4 forces the first operand to be an integer and is therefore valid, resulting in the integer remainder 3. Note that the explicit type specification applies only to the first operand, not the entire expression.
  • 15. EXAMPLE 3.6 Suppose that f is a floating-point variable whose value is 5.5. The expression ((int) f) % 2 contains two integer operands and is therefore valid, resulting in the integer remainder 1. Note, however, that f remains a floating-point variable whose value is 5.5, even though the value off was converted to an integer (5) when carrying out the remainder operation.
  • 16. Precedence of Operators ● The operators within C are grouped hierarchically according to their precedence (i.e., order of evaluation). ● Operations with a higher precedence are carried out before operations having a lower precedence. ● Precedence Level: ○ Brackets ○ Division/Multiplication/ Modulus (Left to right associativity) ○ Addition/Subtraction (Left to Right Associativity)
  • 17. Precedence Level: • Brackets • Division/Multiplication Modulus • Addition/Subtraction
  • 18. Precedence Level: • Brackets • Division/Multiplication/ Modulus • Addition/Subtraction
  • 20. Unary Operators ● C includes a class of operators that act upon a single operand to produce a new value. ● Types of unary operators 1. Unary minus ( – ) 2. Increment ( ++ ) 3. Decrement ( — ) 4. NOT ( ! ) 5. Addressof operator ( & ) 6. sizeof()
  • 21. 1. 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
  • 22. 2. Increment The increment operator ( ++ ) is used to increment the value of the variable by 1. The increment can be done in two ways: 2.1 prefix increment In this method, the operator precedes the operand (e.g., ++a). The value of the operand will be altered before it is used. Example: int a = 1; int b = ++a; // b = 2
  • 23. 2.2 postfix increment In this method, the operator follows the operand (e.g., a++). The value operand will be altered after it is used. Example: int a = 1; int b = a++; // b = 1 int c = a; // c = 2
  • 24. ● Prefix Increment (initial value of i is 1) ● Output • Postfix Increment
  • 25. 3. Decrement ● The decrement operator ( — ) 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 the operand will be altered before it is used. Example: int a = 1; int b = --a; // b = 0
  • 26. ● postfix decrement ● In this method, the operator follows the operand (e.g., a- -). The value of the operand will be altered after it is used. Example: int a = 1; int b = a--; // b = 1 int c = a; // c = 0
  • 27. 4. NOT ( ! ) The logical NOT operator ( ! ) is used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false. Example: If x is true, then !x is false If x is false, then !x is true
  • 28. 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.
  • 33. Relational and equality operators always assign the value 0 or 1. Condition is true= 1 Condition is false=0
  • 39. Assignment Operators ● There are several different assignment operators in C. ● It assign the value of an expression to an identifier. ● The most commonly used assignment operator is =. Assignment expressions that make use of this operator are written in the form ● identifier = expression ● where identifier generally represents a variable, and expression represents a constant, a variable or a more complex expression.
  • 46. Conditional Operators 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
  • 51. x= y = =z The crux of the question lies in the statement x = y==z. The operator == is executed before = because precedence of comparison operators (<=, >= and ==) is higher than assignment operator =. The result of a comparison operator is either 0 or 1 based on the comparison result. int x, y = 5, z = 5;
  • 53. Let us consider the condition inside the if statement. Since there are two greater than (>) operators in the expression “c > b > a”, associativity of > is considered. Associativity of > is left to right. So, expression c > b > a is evaluated as ( (c > b) > a ). And since the (c > b) is being the relational operator it will return 1 if True otherwise 0 is if False.
  • 55. If value of a,b,and c =50, 10, 20 then find the value of above expression.