0% found this document useful (0 votes)
41 views52 pages

SPL Lecture 2

The document discusses various C programming concepts including constants, variables, data types, operators, statements and expressions. It provides examples and explanations of integer, floating-point and character constants as well as different operators such as arithmetic, relational, logical and bitwise operators.

Uploaded by

Rakib Mia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views52 pages

SPL Lecture 2

The document discusses various C programming concepts including constants, variables, data types, operators, statements and expressions. It provides examples and explanations of integer, floating-point and character constants as well as different operators such as arithmetic, relational, logical and bitwise operators.

Uploaded by

Rakib Mia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Structured Programming Language

ICT 1103

Professor Dr. Mohammad Abu


Yousuf

1/31/2023 1
Constants

Constants

Numeric Character
Constants Constants

integer floating-point single string


character

2
Integer Constants

• Consists of a sequence of digits, with possibly a plus or


a minus sign before it.
– Embedded spaces, commas and non-digit characters
are not permitted between digits.

• Maximum and minimum values (for 32-bit


representations)
Maximum :: 2147483647
Minimum :: – 2147483648
3
Floating-point Constants
• Can contain fractional parts.

• Very large or very small numbers can be represented.


23000000 can be represented as 2.3e7
e means “10 to the
• Two different notations: power of”
1. Decimal notation
25.0, 0.0034, .84, -2.234
2. Exponential (scientific) notation
3.45e23, 0.123e-12, 123E2

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

scanf (“%f %f”, &speed, &time);


distance = speed * time;
printf (“\n The distance traversed is: %f”, distance);
}

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

Arithmetic Relational Logical Bitwise


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

• For operators of the same priority, evaluation is from


left to right as they appear.

• Parenthesis may be used to change the precedence of


operator evaluation. 19
Examples: Arithmetic expressions
a+b*c–d/e a + (b * c) – (d / e)
a*–b+d%e–f a * (– b) + (d % e) – f
a–b+c+d (((a – b) + c) + d)
x*y*z ((x * y) * z)
a+b+c*d*e (a + b) + ((c * d) * e)

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

• Used to compare two quantities.

< is less than


> is greater than
<= is less than or equal to
>= is greater than or equal to
== is equal to
!= is not equal to

22
Relational Operators: Examples
10 > 20 is false
25 < 35.5 is true
12 > (7 + 5) is false

• When arithmetic expressions are used on either side of


a relational operator, the arithmetic expressions will be
evaluated first and then the results compared.

a + b > c – d is the same as (a+b) > (c+d)

23
Relational Operators: Examples

• Sample code segment in C

if (x > y)
printf (“%d is larger\n”, x);
else
printf (“%d is larger\n”, y);

24
Logical Operators

• There are two logical operators in C (also called logical


connectives).
&& Logical AND
|| Logical OR

• What they do?


– They act upon operands that are themselves logical
expressions.
– The individual logical expressions get combined into
more complex conditions that are true or false.
25
Logical Operators (Cont..)
– Logical AND
• Result is true if both the operands are true.
– Logical OR
• Result is true if at least one of the operands are true.

X Y X && Y X || Y

FALSE FALSE FALSE FALSE


FALSE TRUE FALSE TRUE

TRUE FALSE FALSE TRUE

TRUE TRUE TRUE TRUE

26
Bitwise Operators

• The bitwise operators are the operators used to perform


the operations on the data at the bit-level.

Operator Meaning of operator


& Bitwise AND operator
| Bitwise OR operator
^ Bitwise exclusive OR operator
~ One's complement operator (unary operator)
<< Left shift operator
>> Right shift operator

27
Bitwise Operators

• Truth table of bitwise operators

X Y X&Y X|Y X^Y


0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

28
Bitwise AND
For example:

We have two variables a and b. 1.#include <stdio.h>


2.int main()
a =6;
3.{
b=4; 4. int a=6, b=4; // variable
The binary representation of the 5. printf("The output is %d",a&b);
above two variables are given below 6. return 0;
: 7.}
a= 0110
b= 0100
____________ Output: The output is 4
a&b = 0100

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)

Bit Operation of 12 and 25

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

The | operator compares corresponding bits of two operands. If either of the


bits is 1, it gives 1. If not, it gives 0. For example,

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25


00001100
| 00011001
________
00011101 = 29 (In decimal)
32
Bitwise XOR

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

The ^ operator compares corresponding bits of two operands. If


corresponding bits are different, it gives 1. If corresponding bits are same,
it gives 0.

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.

35 = 00100011 (In Binary)


Bitwise complement Operation of 35

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

Output: The output is -36

Why are we getting output -36 instead of 220?


It's because the compiler is showing 2's complement of that
number; negative notation of the binary number.
36
Bitwise Complement
For any integer n, 2's complement of n will be -(n+1).

The bitwise complement of 35 is 220 (in decimal). The 2's


complement of 220 is -36. Hence, the output is -36 instead of 220.
37
left shift, right shift
A= 00111100

<< (left shift)


• Binary Left Shift Operator. The left operands value is
moved left by the number of bits specified by the right
operand
Example: A << 2 will give 240 which is 1111 0000.
>> (right shift)
• Binary Right Shift Operator. The left operands value is
moved right by the number of bits specified by the right
operand.
Example: A >> 2 will give 15 which is 1111
38
left shift, right shift
1.#include <stdio.h>
2.int main()
3.{
4. int a=5; // variable declaration
5. printf("The value of a<<2 is : %d ", a<<2);
6. return 0;
7.}
1.#include <stdio.h>
2.int main()
3.{
4. char b = ‘A’; // variable declaration
5. printf("The value of A<<2 is : %d ", A<<2);
6. return 0;
7.}
39
Other Operators
• Some unary operators: -, ++, --, !
Example : ++a, --b
• C contains the following five additional assignment operators:
+=, -= , *=, /= and %=.
• To see how they are used, consider the first operator, +=. The
assignment expression
expression 1 += expression 2
is equivalent to
expression 1 = expression 1 + expression 2
• Similarly, the assignment expression
expression I -= expression 2
is equivalent to
expression 1 = expression I - expression 2
and so on for all five operators.

40
Other Operators

• Suppose that i and j are integer variables whose values


are 5 and 7, and f and g are floating-point variables
whose values are 5.5 and -3.25.

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); // (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

• The C language is accompanied by a number of


library functions that carry out various commonly
used operations or calculations.
• A library function is accessed simply by writing the
function name, followed by a list of arguments that
represent information being passed to the 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).

• Note that P is encoded as (decimal) 80, T is encoded


as 84, and 5 is encoded as 53 in the ASCII character
set
49
Comments in C
• Comments in C language are used to provide information
about lines of code. It is widely used for documenting
code. There are 2 types of comments in the C language.
1. Single Line Comments
2. Multi-Line Comments

• Single Line Comments


Single line comments are represented by double slash //.
1.#include<stdio.h>
2.int main(){
3. //printing information
4. printf("Hello C");
5. return 0;
6.} 50
Comments in C
• Mult Line Comments
• Multi-Line comments are represented by slash asterisk \*
... *\. It can occupy many lines of code, but it can't be
nested. Syntax:
/*
code
to be commented
*/ 1.#include<stdio.h>
2.int main(){
3. /*printing information
4. Multi-Line Comment*/
5. printf("Hello C");
6. return 0;
7.}
51
Thank You

52

You might also like