SPL Lecture 2
SPL Lecture 2
ICT 1103
1/31/2023 1
Constants
Constants
Numeric Character
Constants Constants
2
Integer Constants
4
Single Character Constants
• Contains a single character enclosed within a pair of
single quote marks.
– Examples :: ‘2’, ‘+’, ‘Z’
• Some special backslash characters
‘\n’ new line
‘\t’ horizontal tab
‘\’’ single quote
‘\”’ double quote
‘\\’ backslash
‘\0’ null
5
String Constants
• Sequence of characters enclosed in double quotes.
– The characters may be letters, numbers, special
characters and blank spaces.
• Examples:
“nice”, “Good Morning”, “3+6”, “3”, “C”
6
Escape Sequences
• There are certain characters in C when they are preceded by a
backslash they will have special meaning.
Sequence Meaning
\a Bell (alert)
\b Backspace
\n Newline
\t Horizontal tab
\\ Backslash
\' Single quote
\" Double quotation
\xhh ASCII char specified by hex digits hh
\ooo ASCII char specified by octal digits ooo
7
Example: Constant (The const keyword)
Syntax: const type variable = value;
1.#include<stdio.h>
2.int main(){
3. const float PI=3.14;
4. printf("The value of PI is: %f",PI);
5. return 0;
6.}
Output:
The value of PI is: 3.140000
8
Example: Constant (The const keyword)
• If you try to change the value of PI, it will render compile
time error.
1.#include<stdio.h>
2.int main(){
3. const float PI=3.14;
4. PI=4.5;
5. printf("The value of PI is: %f",PI);
6. return 0;
7.}
Output:
Compile Time Error: Cannot modify a const object
9
Declaration of Variables
• There are two purposes:
1. It tells the compiler what the variable name is.
2. It specifies what type of data the variable will
hold.
• Syntax:
data-type variable_1,
variable_2,……..,variable_n;
•
• Examples:
int velocity, distance;
int a, b, c, d;
float temp;
char flag, option;
10
An Example : Variable Declaration
#include <stdio.h>
main()
{
float speed, time, distance; Variable declaration
11
Expression
• An expression represents a single data item, such as a
number or a character. The expression may consist of a
single entity, such as a constant, a variable, an array
element or a reference to a function. It may also consist
of some combination of such entities, interconnected by
one or more operators.
• Several simple expressions are shown below.
a+b
x=y
c=a+b
x <= y
x == Y
++i 12
Statement
• A statement causes the computer to carry out some
action. There are three different classes of statements in C.
• They are expression statements, compound statements
and control statements.
Expression Statement:
• An expression statement consists of an expression
followed by a semicolon.
a = 3; // Assignment statement
c = a + b ; // Assignment statement
++i; // Incremental statement
p r i n t f ("Area = %f”, area);
13
Statement (Cont..)
Compound Statement:
• A compound statement consists of several individual
statements enclosed within a pair of braces { }.
• A typical compound statement is shown below.
{
p i = 3.141593;
circumference = 2 * pi * radius;
area = pi * radius * radius;
}
14
Statement (Cont..)
Control Statement:
• Control statements are used to create special
program features, such as logical tests, loops and
branches.
Example:
while (count <= n) {
printf ( ' x = ' ) ;
scanf ( “%f" , &x) ;
sum += x;
++count;
} 15
Operators
Operators
16
Arithmetic Operators
• Addition :: +
• Subtraction :: –
• Division :: /
• Multiplication :: *
• Modulus :: %
Examples:
distance = rate * time ;
netIncome = income - tax ;
speed = distance / time ;
area = PI * radius * radius;
y = a * x * x + b*x + c;
quotient = dividend / divisor;
remain =dividend % divisor; 17
Arithmetic Operators(Cont..)
• Suppose x and y are two integer variables, whose
values are 13 and 5 respectively.
x+y 18
x–y 8
x*y 65
x/y 2
x%y 3
18
Operator Precedence
• In decreasing order of priority
1. Parentheses :: ( )
2. Unary minus :: –5
3. Multiplication, Division, and Modulus
4. Addition and Subtraction
20
Type Cast
• The value of an expression can be converted to a
different data type if desired.
• To do so, the expression must be preceded by the
name of the desired data type, enclosed in
parentheses, i.e.,
(data type) expression.
Example:
int x=10;
float y,z=3.14;
y=(float) x; /* y=10.0 */
x=(int) z; /* x=3 */
x=(int) (-z); /* x=-3 -- rounded approaching zero */
21
Relational Operators
22
Relational Operators: Examples
10 > 20 is false
25 < 35.5 is true
12 > (7 + 5) is false
23
Relational Operators: Examples
if (x > y)
printf (“%d is larger\n”, x);
else
printf (“%d is larger\n”, y);
24
Logical Operators
X Y X && Y X || Y
26
Bitwise Operators
27
Bitwise Operators
28
Bitwise AND
For example:
29
Bitwise AND
Bitwise AND is a binary operator (operates on two operands). It's denoted
by &.
The & operator compares corresponding bits of two operands. If both bits are
1, it gives 1. If either of the bits is not 1, it gives 0.
For example,
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
00001100
& 00011001
________
00001000 = 8 (In decimal) 30
Bitwise OR
For example:
We consider two variables, 1.#include <stdio.h>
a = 23; 2.int main()
b = 10; 3.{
4. int a=23,b=10; // variable
The binary representation of the 5. printf("The output is %d",a|b);
above two variables would be: 6. return 0;
a = 00010111 7.}
b = 00001010
___________________
a|b = 00011111 Output: The output is 31
31
Bitwise OR
Bitwise OR is a binary operator (operates on two operands). It's denoted
by |.
For example:
We consider two variables a and 1.#include <stdio.h>
b, 2.int main()
a = 12; 3.{
b = 10; 4. int a=12,b=10; // variable
The binary representation of the 5. printf("The output is %d",a^b);
above two variables would be: 6. return 0;
a= 00001100 7.}
b= 00001010
________________
(a^b) = 0000 1110 Output: The output is 6
33
Bitwise XOR
Bitwise XOR is a binary operator (operates on two operands). It's denoted
by ^.
For example,
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
________
00010101 = 21 (In decimal)
34
Bitwise Complement
Bitwise complement is an unary operator (works on only one operand). It is
denoted by ~.
The ~ operator inverts the bit pattern. It makes every 0 to 1, and every 1 to 0.
~ 00100011
________
11011100 = 220 (In decimal)
35
Bitwise Complement
1.#include <stdio.h>
2.int main()
3.{
4. int a= 35; // variable
5. printf("The output is %d",~a);
6. return 0;
7.}
40
Other Operators
41
Increment and Decrement
Operators
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5; Output:
++a = 11
printf("++a = %d \n", ++a); --b = 99
++c = 11.500000
printf("--b = %d \n", --b);
--d = 99.500000
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0; 42
}
Operator Precedence Groups
43
Operator Precedence in C
#include <stdio.h>
main()
{ int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
} 44
Operator Precedence in C
Output:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
45
Library function
46
Library function (Cont..)
Some commonly used library function
47
Library function (Cont..)
48
• suppose that c1 and c2 are character-type variables
that represent the characters P and T, respectively.
Several arithmetic expressions that make use of
these variables are shown below, together with their
resulting values (based upon the ASCII character set).
52