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
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.
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
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.
Relational and Logical Operators
Relational and equality operators always
assign the value 0 or 1.
Condition is true= 1
Condition is false=0
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.
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
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;
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.
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
Chapter ends here

introduction to c programming - Topic 3.pdf

  • 1.
    Introduction to Programming(ECS 102) Instructors: Dr. Jasabanta Patro and Dr. Rini Smita Thakur
  • 2.
  • 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 andModulus 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 theModulus Operator ● % modulus operator cannot be applied to floating-point numbers i.e. float or double.
  • 10.
    Modulus Operation withNegative Operand Here for a % b, the sign of left operand is appended to the result.
  • 11.
    Ans: -3 Most versionsof 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 iis 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 Supposethat 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 ● Cincludes 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 Theminus 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 incrementoperator ( ++ ) 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 Inthis 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 ● Thedecrement 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 returnsthe size of its operand, in bytes. The sizeof() operator always precedes its operand. The operand is an expression, or it may be a cast.
  • 31.
  • 33.
    Relational and equalityoperators always assign the value 0 or 1. Condition is true= 1 Condition is false=0
  • 39.
    Assignment Operators ● Thereare 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 conditionaloperator 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
  • 50.
  • 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 considerthe 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 ofa,b,and c =50, 10, 20 then find the value of above expression.
  • 56.
  • 57.
  • 58.
  • 66.