introduction to c programming - Topic 3.pdf

rajd20284 28 views 66 slides Jun 17, 2024
Slide 1
Slide 1 of 66
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66

About This Presentation

introduction to c programming


Slide Content

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

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.


Since y is equal to z, value of the expression y == z
becomes 1 and the value is assigned to x via the
assignment operator.
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.



So here the value returned is 1 and then it is compared to
the a. so now, the statement becomes, (1 > a), which is
false, so the answer, return is 0, therefore, else part is
executed.

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