0% found this document useful (0 votes)
8 views

Introduction To Computer Programming With C Language: Operators and Constants

The document discusses various operators in C programming such as arithmetic, relational, logical, and bitwise operators and explains how to define and use constants. It provides examples of using operators with constants and variables and covers operator precedence and associativity. Constants refer to fixed values that cannot change during program execution while operators allow performing mathematical and logical functions on constants and variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Introduction To Computer Programming With C Language: Operators and Constants

The document discusses various operators in C programming such as arithmetic, relational, logical, and bitwise operators and explains how to define and use constants. It provides examples of using operators with constants and variables and covers operator precedence and associativity. Constants refer to fixed values that cannot change during program execution while operators allow performing mathematical and logical functions on constants and variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Constants and

Operators
Mahit Kumar Paul
Assistant Professor, Dept. of CSE
RUET, Rajshahi-6204
mahit.cse@gmail.com

2023-10-08 1
Constants and Literals
• Constants refer to fixed values that the program may
not alter during its execution.

• These fixed values are also called literals.

2023-10-08 2
Defining Constants
Using #define Preprocessor

Format: #define identifier value

When we use define for a constant, the preprocessor


produces a C program where the defined constant is
searched and matching tokens are replaced with the
given expression.

2023-10-08 5
Defining Constants-Example
Example

Output: value of area : 50


2023-10-08 6
Operators
• An operator is a symbol that tells the compiler to
perform specific mathematical or logical functions.
• Types:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators

2023-10-08 9
Arithmetic Operators
• Assume variable A holds 10 and variable B holds 20

2023-10-08 10
Relational Operators
• Assume variable A holds 10 and variable B holds 20

2023-10-08 11
Logical Operators
• Assume variable A holds 52 and variable B holds 0

2023-10-08 12
Logical Operators…
Example

Output: Line 1 - Condition is true


Line 2 - Condition is true
2023-10-08 13
Bitwise Operators
• Assume variable A holds 60 and variable B holds 13

2023-10-08 14
Bitwise Operators-Example
Example

2023-10-08 15
Bitwise Operators-Example…
Output

Value of Binary AND = 12


Value of Binary OR = 61
Value of Binary XOR = 49
Value of Binary LEFT SHIFT = 240
Value of Binary RIGHT SHIFT = 15

2023-10-08 16
Assignment Operators

2023-10-08 17
Assignment Operators…

2023-10-08 18
Other Operators…

2023-10-08 19
Sizeof() Operator

Output:

Value = 9

2023-10-08
Memory byte of z = 4 20
& Operator

Output: Value = 9

Memory byte of z = 4

2023-10-08 Memory Address of z is = 60ff04 21


?: Operator
• Called conditional operator
• Also called ternary operator

Syntax

If Condition is true ? then value X : otherwise value Y

2023-10-08 22
?: Operator…
Example
• The following program prints the biggest number
between two integer numbers.

Output: Value of z = 7
2023-10-08 23
Operator Precedence and Associativity
• Operator precedence helps us determine which of the
operators in an expression must be evaluated first in case
the expression consists of more than a single operator.

• Explanation: x = 7 + 3 * 2; here, x is assigned 13,


not 20 because operator * has a higher precedence than
+, so it first gets multiplied with 3*2 and then adds into
7.
2023-10-08 24
Operator Precedence and Associativity…
• We use associativity when two or more than two
operators with the same precedence are present in the
same expression.

• Explanation: The precedence of Division and


Multiplication arithmetic operators is the same. So, let’s say
we have an expression with us which is 6 * 3 / 20. The
evaluation of this expression would be (6 * 3) / 20 because
the associativity will be left to right for both the operators –
multiplication and division. In a similar case, the calculation
of 40 / 4 * 5 would be (40 / 4) * 5 because the associativity
would be from left to right here as well.

2023-10-08 25
Operator Precedence and Associativity…

2023-10-08 26
2023-10-08 27

You might also like