0% found this document useful (0 votes)
2 views120 pages

4 Basic Refreshers

Uploaded by

mktree897
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)
2 views120 pages

4 Basic Refreshers

Uploaded by

mktree897
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/ 120

Basic Refreshers

Number System
Advanced C
Number Systems
Type Dec Oct Hex Bin

A number is generally Base 10 8 16 2
represented as 0 0 0 0 0 0 0

– Decimal 1
2
1
2
1
2
0 0 0 0
0 0 1
1
0 0
– Octal 3 3 3 0 0 1
0 1
0
4 4 4 0 1
0 0 0
– Hexadecimal 5 5 5 0 1
0 0 1
0
6 6 6 0 1
0 1
0 0
– Binary 7 7 7 0 1
0 1
0 1
0
8 10 8 0 0 0 0
1
9 11 9 0 0 0 1
1 0
10 12 A 0 0 1
1 0 0
Type Range (8 Bits)
11 13 B 0 0 1
1 0 1
0
Decimal 0 - 255
12 14 C 0 1
1 0 0 0
Octal 000 - 0377
13 15 D 0 1
1 0 0 1
0
Hexadecimal 0x00 - 0xFF
14 16 E 0 1
1 0 1
0 0
Binary 0b00000000 - 0b11111111
15 17 F 0 1
1 0 1
0 1
0
Advanced C
Number Systems – Decimal to Binary

● 12510 to Binary

2 125
2 62 1 LSB
2 31 0
2 15 1
2 7 1
2 3 1
2 1 1
0 1 MSB
● So 12510 is 11111012
Advanced C
Number Systems – Decimal to Octal

● 21210 to Octal

8 212
8 26 4 LSB
8 3 2
0 3 MSB

● So 21210 is 3248
Advanced C
Number Systems – Decimal to Hexadecimal

● 47210 to Hexadecimal

16 472
16 29 8 LSB
16 1 13
0 1 MSB

Representation Substitutes
Dec 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F

● So 47210 is 1D816
Advanced C
Number Systems – Hexadecimal to Binary

● 1D816 to Binary

1 D 8

1 D 8

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

● So 1D816 is 0001110110002 which is nothing but 111011000 2


Advanced C
Number Systems – Octal to Binary

● 3248 to Binary

3 2 4

3 2 4

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

● So 3248 is 0110101002 which is nothing but 110101002


Advanced C
Number Systems – Octal to Hexadecimal

● 3248 to Hexadecimal
3 2 4

3 2 4

0 1 1 0 1 0 1 0 0

0 1 1 0 1 0 1 0 0

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

0 D 4
● So 3248 is 0D416 which is nothing but D416
Advanced C
Number Systems – Hexadecimal to Octal

● 1D816 to Octal
1 D 8

1 D 8

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

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

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

0 7 3 0
● So 1D816 is 07308 which is nothing but 7308
Advanced C
Number Systems – Binary to Decimal

● 1110110002 to Decimal
1 1 1 0 1 1 0 0 0

Bit Position 8 7 6 5 4 3 2 1 0

8 7 6 5 4 3 2 1 0
2 2 2 2 2 2 2 2 2

256 + 128 + 64 + 0 + 16 + 8 + 0 + 0 + 0 =

472

● So 1110110002 is 47210
Data Representations
Advanced C
Data Representation - Bit


Literally computer understand only two states HIGH and
LOW making it a binary system

These states are coded as 1 or 0 called binary digits

“Binary Digit” gave birth to the word “Bit”

Bit is known a basic unit of information in computer and
digital communication

Value No of Bits
0 0
1 1
Advanced C
Data Representation - Byte


A unit of digital information

Commonly consist of 8 bits

Considered smallest addressable unit of memory in
computer

Value No of Bits
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1
Advanced C
Data Representation - Character


One byte represents one unique character like 'A', 'b', '1',
'$' ...

Its possible to have 256 different combinations of 0s and
1s to form a individual character

There are different types of character code
representation like
– ASCII → American Standard Code for Information
Interchange – 7 Bits (Extended - 8 Bits)
– EBCDIC → Extended BCD Interchange Code – 8 Bits
– Unicode → Universal Code - 16 Bits and more
Advanced C
Data Representation - Character


ASCII is the oldest representation

Please try the following on command prompt to know the
available codes
$ man ascii

Can be represented by char datatype

Value No of Bits
0 0 0 1 1 0 0 0 0
A 0 1 0 0 0 0 0 1
Advanced C
Data Representation - word


Amount of data that a machine can fetch and process at
one time

An integer number of bytes, for example, one, two, four,
or eight

General discussion on the bitness of the system is
references to the word size of a system, i.e., a 32 bit
chip has a 32 bit (4 Bytes) word size

Value No of Bits
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
Advanced C
Integer Number - Positive


Integers are like whole numbers, but allow negative
numbers and no fraction
● An example of 1310 in 32 bit system would be
Bit No of Bits
Position 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
Advanced C
Integer Number - Negative


Negative Integers represented with the 2's complement of
the positive number
● An example of -1310 in 32 bit system would be
Bit No of Bits
Position 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1

1's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0
Add 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

2's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1

n

Mathematically : -k ≡ 2 - k
Advanced C
Float Point Number


A formulaic representation which approximates a real
number

Computers are integer machines and are capable of
representing real numbers only by using complex codes

The most popular code for representing real numbers is
called the IEEE Floating-Point Standard
Sign Exponent Mantissa
Float (32 bits)
1 bit 8 bits 23 bits
Single Precision
Double (64 bits)
1 bit 11 bits 52 bits
Double Precision
Advanced C
Float Point Number – Conversion Procedure

STEP 1: Convert the absolute value of the number to binary, perhaps
with a fractional part after the binary point. This can be done by -
– Converting the integral part into binary format.
– Converting the fractional part into binary format.
The integral part is converted with the techniques examined
previously.
The fractional part can be converted by multiplying it with 2.

STEP 2: Normalize the number. Move the binary point so that it is
one bit from the left. Adjust the exponent of two so that the value
does not change.

Float : V = (-1)s * 2(E-127) * 1.F


Double : V = (-1)s * 2(E-1023) * 1.F
Advanced C
Float Point Number – Conversion - Example 1
Convert 0.5 to IEEE 32-bit floating point format
Step 1: Step 2:

0.5 ×2 1.0 1 Normalize:


0.12 = 1.02 × 2-1
0.510 = 0.12 Mantissa is 00000000000000000000000
Exponent is -1 + 127 = 126 = 011111102
Sign bit is 0

Bit S Exponent Mantissa


Position 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Value 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Advanced C
Float Point Number – Conversion - Example 2
Convert 8.25 to IEEE 32-bit floating point format
Step 1: Step 2:
Integer Part to Binary: Normalize:
8 /2 4 0 1000.012 = 1.000012 × 23
4 /2 2 0 Mantissa is 00001000000000000000000
Exponent is 3 + 127 = 130 = 1000 00102
2 /2 1 0
Sign bit is 0
1 1
Fractional Part to Binary:
0.25 ×2 0.5 0
0.5 ×2 1.0 1
Result:
8.2510 = 1000.012
Bit S Exponent Mantissa
Position 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Value 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Advanced C
Float Point Number – Conversion - Example 3
Convert 0.625 to IEEE 32-bit floating point format
Step 1: Step 2:

0.625 ×2 1.25 1 Normalize:


0.1012 = 1.012 × 2-1
0.25 ×2 0.5 0
Mantissa is 01000000000000000000000
0.5 ×2 1.0 1 Exponent is -1 + 127 = 126 = 011111102
Sign bit is 0
0.62510 = 0.1012

Bit S Exponent Mantissa


Position 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Value 0 0 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Advanced C
Float Point Number – Conversion - Example 4
Convert 39887.5625 to IEEE 32-bit floating point format
Step 1: Step 2:

0.5625 ×2 1.125 1 Normalize:


1001101111001111.10012 =
0.125 ×2 0.25 0 15
1.00110111100111110012 × 2
0.25 ×2 0.5 0
0.5 ×2 1.0 1 Mantissa is 00110111100111110010000
Exponent is 15 + 127 = 142 = 100011102
39887.562510 = Sign bit is 0

1001101111001111.10012

Bit S Exponent Mantissa


Position 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Value 0 1 0 0 0 1 1 1 0 0 0 1 1 0 1 1 1 1 0 0 1 1 1 1 1 0 0 1 0 0 0 0
Advanced C
Float Point Number – Conversion - Example 6
Convert -13.3125 to IEEE 32-bit floating point format
Step 1: Step 2:

0.3125 ×2 0.625 0 Normalize:


1101.01012 = 1.10101012 × 23
0.625 ×2 1.25 1
0.25 ×2 0.5 0
0.5 ×2 1.0 1 Mantissa is 10101010000000000000000
Exponent is 3 + 127 = 130 = 1000 00102
13.312510 = 1101.01012 Sign bit is 1

Bit S Exponent Mantissa


Position 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Value 1 1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Advanced C
Float Point Number – Conversion - Example 8
Convert 1.7 to IEEE 32-bit floating point format
Step 1: Step 2:

0.7 ×2 1.4 1 Normalize:


1.101100110011001100110012 =
0.4 ×2 0.8 0 0
1.101100110011001100110012 × 2
0.8 ×2 1.6 1
0.6 ×2 1.2 1 Mantissa is 10110011001100110011001
Exponent is 0 + 127 = 127 = 011111112
0.2 x2 0.4 0
Sign bit is 0
0.4 ×2 0.8 0
0.8 ×2 1.6 1
0.6 ×2 1.2 1
1.710 = 1.101100110011001100110012

Bit S Exponent Mantissa


Position 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Value 0 0 1 1 1 1 1 1 1 1 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1
Data Types
Advanced C
Data Types - Categories

char

Integral

int
Data
Types
float

Real

double
Advanced C
Data Types - Usage
Syntax
data_type name_of_the_variable;

Example
char option;
int age;
float height;
Advanced C
Data Types – Storage
Example 0000

char option; 0004


int age;
float height; 0008

1000

1004

1008

1012

1016

1020
Advanced C
Data Types – Printing
001_example.c 0000
#include <stdio.h>
0004
int main()
{ 0008
char option;
int age;
float height; ●

printf(“The character is %c\n”, option); ●

printf(“The integer is %d\n”, age); ●

printf(“The float is %f\n”, height);


1000
return 0;
}
1004

1008

1012

1016

1020
Advanced C
Data Types – Scaning
002_example.c 0000
#include <stdio.h>
0004
int main()
{ 0008
char option;
int age;
float height;

scanf(“%c”, &option); ●

printf(“The character is %c\n”, option); ●


scanf(“%d”, &age);
printf(“The integer is %d\n”, age);
1000
scanf(“%f”, &height);
printf(“The float is %f\n”, height);
1004
return 0;
} 1008

1012

1016

1020
Advanced C
Data Types – Size
003_example.c 0000
#include <stdio.h>
0004
int main()
{ 0008
char option;
int age;
float height;

printf(“The size of char is %u\n”, sizeof(char)); ●

printf(“The size of int is %u\n”, sizeof(int)); ●


printf(“The float is %u\n”, sizeof(float));
1000
return 0;
}
1004

1008

1012

1016

1020
Advanced C
Data Types - Modifiers and Qualifiers


These are some keywords which is used to tune the
property of the data type like
– Its width
– Its sign
– Its storage location in memory
– Its access property

The K & R C book mentions only two types of qualifiers
(refer the next slide). The rest are sometimes
interchangably called as specifers and modifiers and
some people even call all as qualifiers!
Advanced C
Data Types - Modifiers and Qualifiers

short T
Size
long T
Modifiers
signed T
Signedness
unsigned T

const V
Qualifiers
volatile V V Variables

T Data Types

Note: Unsigned float in not supported F Functions


Advanced C
Data Types - Modifiers and Qualifiers - Usage
Syntax
<modifier> <qualifier> <data_type> name_of_the_variable;

Example
short int count1;
long int count2;
const int flag;
Advanced C
Data Types – Modifiers - Size
004_example.c 0000
#include <stdio.h>
0004
int main()
{ 0008
short int count1;
int long count2;
short count3; ●


printf(“short is %u bytes\n”, sizeof(short int)); ●
printf(“long int is %u bytes\n”, sizeof(int long));
printf(“short is %u bytes\n”, sizeof(short));
1000
return 0;
} 1004

1008

1012

1016

1020
Advanced C
Data Types – Modifiers - Sign
005_example.c 0000
#include <stdio.h>
0004
int main()
{ 0008
unsigned int count1;
signed int count2;
unsigned char count3; ●

signed char count4; ●


printf(“count1 is %u bytes\n”, sizeof(unsigned int));
printf(“count2 is %u bytes\n”, sizeof(signed int));
printf(“count3 is %u bytes\n”, sizeof(unsigned char)); 1000
printf(“count3 is %u bytes\n”, sizeof(signed char));
1004
return 0;
} 1008

1012

1016

1020
Advanced C
Data Types – Modifiers – Sign and Size
006_example.c 0000
#include <stdio.h>
0004
int main()
{ 0008
unsigned short count1;
signed long count2;
short signed count3; ●


printf(“count1 is %u bytes\n”, sizeof(count1)); ●
printf(“count2 is %u bytes\n”, sizeof(count2));
printf(“count3 is %u bytes\n”, sizeof(count3));
1000
return 0;
} 1004

1008

1012

1016

1020
Advanced C
Data Types – Modifiers – Sign and Size
007_example.c
#include <stdio.h>

int main()
{
unsigned int count1 = 10;
signed int count2 = -1;

if (count1 > count2)


{
printf(“Yes\n”);
}
else
{
printf(“No\n”);
}

return 0;
}
Advanced C
Data Types and Function - Storage Modifiers

auto V

static V F
Storage
Modifiers
extern V F

register V

inline F
V Variables

T Data Types

F Functions
Statements
Advanced C
Code Statements - Simple
int main()
{
number = 5; Assignment statement
3; +5;
sum = number + 5; Valid statement, But smart compilers
might remove it
4 + 5;
; Assignment statement. Result of the
} number + 5 will be assigned to sum

Valid statement, But smart compilers


might remove it

This valid too!!


Advanced C
Code Statements - Compound
int main()
{
...
if (num1 > num2) If conditional statement
{
if (num1 > num3) Nested if statement
{
printf(“Hello”);
}
else
{
printf(“World”);
}
}
...
}
Conditional Constructs
Advanced C
Conditional Constructs

if and its family


Single iteration
switch case
Conditional
Constructs for

Multi iteration while

do while
Advanced C
Conditional Constructs - if
Syntax 008_example.c
if (condition) #include <stdio.h>
{
statement(s); int main()
{
}
int num = 2;

Flow if (num < 5)


{
printf(“num < 5\n”);
}
printf(“num is %d\n”, num);
true
cond? return 0;
}
false
code
Advanced C
Conditional Constructs – if else
Syntax Flow
if (condition)
{
statement(s);
}
else true
{ cond?
statement(s);
} false

code code
Advanced C
Conditional Constructs – if else
009_example.c
#include <stdio.h>

int main()
{
int num = 10;

if (num < 5)
{
printf(“num is smaller than 5\n”);
}
else
{
printf(“num is greater than 5\n”);
}

return 0;
}
Advanced C
Conditional Constructs – if else if
Syntax Flow
if (condition1)
{
statement(s);
} true
else if (condition2) cond1? code
{
statement(s); false
} true
else
cond2? code
{
statement(s); false
}
code
Advanced C
Conditional Constructs – if else if
010_example.c
#include <stdio.h>

int main()
{
int num = 10;

if (num < 5)
{
printf(“num is smaller than 5\n”);
}
else if (num > 5)
{
printf(“num is greater than 5\n”);
}
else
{
printf(“num is equal to 5\n”);
}

return 0;
}
Advanced C
Conditional Constructs – Exercise


WAP to find the max of two numbers

WAP to print the grade for a given percentage

WAP to find the greatest of given 3 numbers

WAP to check whether character is
– Upper case
– Lower case
– Digit
– No of the above

WAP to find the middle number (by value) of given 3
numbers
Advanced C
Conditional Constructs – switch
Syntax Flow
switch (expression)
{
case constant:
statement(s); expr
break;
case constant:
statement(s); true
break; case1? code break
case constant:
statement(s); false
break; true
default: case2? code break
statement(s);
} false

default code break


Advanced C
Conditional Constructs - switch
011_example.c
#include <stdio.h>

int main()
{
int option;
printf(“Enter the value\n”);
scanf(“%d”, &option);

switch (option)
{
case 10:
printf(“You entered 10\n”);
break;
case 20:
printf(“You entered 20\n”);
break;
default:
printf(“Try again\n”);
}

return 0;
}
Advanced C
Conditional Constructs – switch - DIY


W.A.P to check whether character is
– Upper case
– Lower case
– Digit
– None of the above

W.A.P for simple calculator
Advanced C
Conditional Constructs – while
Syntax Flow
while (condition)
{

Controls the loop.
statement(s);

Evaluated before each
} execution of loop body

012_example.c
false
#include <stdio.h>
cond?
int main()
{ true
int iter; code

iter = 0;
while (iter < 5)
{
printf(“Looped %d times\n”, iter);
iter++;
}

return 0;
}
Advanced C
Conditional Constructs – do while
Syntax Flow
do
{

Controls the loop.
statement(s);

Evaluated after each
} while (condition); execution of loop body

013_example.c
#include <stdio.h> code

int main() true


{
int iter; cond?

iter = 0;
do false
{
printf(“Looped %d times\n”, iter);
iter++;
} while (iter < 10);

return 0;
}
Advanced C
Conditional Constructs – for
Syntax Flow
for (init; condition; post evaluation expr)
{
statement(s); ●
Controls the loop.
} ●
Evaluated before each init
execution of loop body
014_example.c
#include <stdio.h> false
cond?
int main()
{ true
int iter; code

for (iter = 0; iter < 10; iter++)


{ post eval
printf(“Looped %d times\n”, iter); expr
}

return 0;
}
Advanced C
Conditional Constructs - Classwork


W.A.P to print the power of two series using for loop
– 21, 22, 23, 24, 25 ...

W.A.P to print the power of N series using Loops
– N1, N2, N3, N4, N5 ...

W.A.P to multiply 2 nos without multiplication operator

W.A.P to check whether a number is palindrome or not
Advanced C
Conditional Constructs – for – DIY


WAP to print line pattern
– Read total (n) number of pattern chars in a line (number
should be “odd”)
– Read number (m) of pattern char to be printed in the
middle of line (“odd” number)
– Print the line with two different pattern chars
– Example – Let's say two types of pattern chars '$' and '*' to
be printed in a line. Total number of chars to be printed in
a line are 9. Three '*' to be printed in middle of line.

Output ==> $$$* * *$$$
Advanced C
Conditional Constructs – for – DIY


Based on previous example print following pyramid

*
***
*****
*******
Advanced C
Conditional Constructs – for – DIY


Print rhombus using for loops

*
***
*****
*******
*****
***
*
Advanced C
Conditional Constructs – break
Flow

A break statement shall appear
only in “switch body” or “loop
body”

“break” is used to exit the loop, code block

the statements appearing after


break in the loop will be skipped
true
cond? break?

true false
Syntax
do
loop
{ cond?
conditional statement
break; false
} while (condition);
Advanced C
Conditional Constructs – break
015_example.c
#include <stdio.h>

int main()
{
int iter;

for (iter = 0; iter < 10; iter++)


{
if (iter == 5)
{
break;
}
printf(“%d\n”, iter);
}

return 0;
}
Advanced C
Conditional Constructs – continue
Flow

A continue statement causes a jump
to the loop-continuation portion,
that is, to the end of the loop body
code block

The execution of code appearing
after the continue will be skipped true
cond? continue?

Can be used in any type of multi
iteration loop false

code block
Syntax
do
loop
{ cond?
conditional statement true
continue; false
} while (condition);
Advanced C
Conditional Constructs – continue
016_example.c
#include <stdio.h>

int main()
{
int iter;

for (iter = 0; iter < 10; iter++)


{
if (iter == 5)
{
continue;
}
printf(“%d\n”, iter);
}

return 0;
}
Advanced C
Conditional Constructs – goto


A goto statement causes a
unconditional jump to a labeled
statement

Generally avoided in general
programming, since it sometimes
becomes tough to trace the flow
of the code

Syntax
goto label;


label:

Operators
Advanced C
Operators


Symbols that instructs the compiler to perform specific
arithmetic or logical operation on operands

All C operators do 2 things
– Operates on its operands
– Returns a value
Advanced C
Operators
unary

Operand binary

ternary

Arithmetic + - * / % ...
Category
Logical ! || && ...

Relational > < <= >= == !=

Operation Assignment = ...


Bitwise & | ^ ~ >> <<

Language sizeof() ...

Pointers * & ->


Advanced C
Operators – Precedence and Associativity
Operators Associativity Precedence
() [] -> . L-R HIGH
! ∼ ++ −− - + * & (type) sizeof R-L
/%* L-R
+- L-R
<< >> L-R
< <= > >= L-R
== != L-R
& L-R
^ L-R Note:
| L-R post ++ and –-
&& L-R operators have
|| L-R higher precedence
than pre ++ and –-
?: R-L operators
= += -= *= /= %= &= ^= |= <<= >>= R-L
, L-R LOW (Rel-99 spec)
Advanced C
Operators - Arithmetic
Operator Description Associativity
/ Division
* Multiplication
% Modulo L to R
+ Addition
- Subtraction

017_example.c
#include <stdio.h>

int main() What will be


{ the output?
int num;

num = 7 - 4 * 3 / 2 + 5;

printf(“Result is %d\n”, num);

return 0;
}
Advanced C
Operators – Language - sizeof()
018_example.c
#include <stdio.h>

int main()
{
int num = 5;

printf(“%u:%u:%u\n”, sizeof(int), sizeof num, sizeof 5);

return 0;
}

019_example.c
#include <stdio.h>

int main()
{
int num1 = 5;
int num2 = sizeof(++num1);

printf(“num1 is %d and num2 is %d\n”, num1, num2);

return 0;
}
Advanced C
Operators – Language - sizeof()


3 reasons for why sizeof is not a function
– Any type of operands
– Type as an operand
– No brackets needed across operands
Advanced C
Type Conversion

Implicit (by compiler)

Type
Conversion

Explicit (by programmer)


Advanced C
Type Conversion Hierarchy

long double
double
float
unsigned long long
signed long long
unsigned long
signed long
unsigned int
signed int
unsigned short
signed short
unsigned char
signed char
Advanced C
Type Conversion - Implicit


Automatic Unary conversions
– The result of + and - are promoted to int if operands are
char and short
– The result of ~ and ! is integer

Automatic Binary conversions
– If one operand is of LOWER RANK (LR) data type & other is
of HIGHER RANK (HR) data type then LOWER RANK will be
converted to HIGHER RANK while evaluating the expression.
– Example: LR + HR → LR converted to HR
Advanced C
Type Conversion - Implicit


Type promotion
– LHS type is HR and RHS type is LR → int = char → LR is promoted
to HR while assigning

Type demotion
– LHS is LR and RHS is HR → int = float → HR rank will be demoted
to LR. Truncated
Advanced C
Type Conversion – Explicit (Type Casting)
Syntax
(data_type) expression

020_example.c
#include <stdio.h>

int main()
{
int num1 = 5, num2 = 3;

float num3 = (float) num1 / num2;

printf(“nun3 is %f\n”, num3);

return 0;
}
Advanced C
Operators - Logical
Operator Description Associativity
021_example.c ! Logical NOT R to L
#include <stdio.h> && Logical AND L to R
|| Logical OR L to R
int main()
{
int num1 = 1, num2 = 0;
What will be
if (++num1 || num2++) the output?
{
printf(“num1 is %d num2 is %d\n”, num1, num2);
}
num1 = 1, num2 = 0;
if (num1++ && ++num2)
{
printf(“num1 is %d num2 is %d\n”, num1, num2);
}
else
{
printf(“num1 is %d num2 is %d\n”, num1, num2);
}
return 0;
}
Advanced C
Operators – Circuit Logical


Have the ability to “short circuit” a calculation if the
result is definitely known, this can improve efficiency
– Logical AND operator ( && )

If one operand is false, the result is false.
– Logical OR operator ( || )

If one operand is true, the result is true.
Advanced C
Operators - Relational
Operator Description Associativity
> Greater than
< Lesser than
>= Greater than or equal
L to R
022_example.c <= Lesser than or equal
== Equal to
#include <stdio.h>
!= Not Equal to
int main()
{
float num1 = 0.7;

if (num1 == 0.7)
{ What will be
printf(“Yes, it is equal\n”);
} the output?
else
{
printf(“No, it is not equal\n”);
}

return 0;
}
Advanced C
Operators - Assignment
023_example.c 024_example.c
#include <stdio.h> #include <stdio.h>

int main() int main()


{ {
int num1 = 1, num2 = 1; float num1 = 1;
float num3 = 1.7, num4 = 1.5;
if (num1 = 1)
num1 += num2 += num3 += num4; {
printf(“Yes, it is equal!!\n”);
printf(“num1 is %d\n”, num1); }
else
return 0; {
} printf(“No, it is not equal\n”);
}

return 0;
}
Advanced C
Operators - Bitwise


Bitwise operators perform operations on bits

The operand type shall be integral

Return type is integral value
Advanced C
Operators - Bitwise

Value
Operand Value
Bitwise ANDing of 0x60
A 0x61 0 1 1 0 0 0 0 1
& Bitwise AND all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
&B 0x01 0 0 0 0 0 0 0 1

Value
Operand Value
Bitwise ORing of 0x60
A 0x61 0 1 1 0 0 0 0 1
| Bitwise OR all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
|B 0x73 0 1 1 1 0 0 1 1
Advanced C
Operators - Bitwise

Value
Operand Value
Bitwise XORing of 0x60
A 0x61 0 1 1 0 0 0 0 1
^ Bitwise XOR all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
^B 0x72 0 1 1 1 0 0 1 0

Complimenting Value
Operand Value

~ Compliment all the bits of the 0x60


A 0x61 0 1 1 0 0 0 0 1
operand
0x13
~A 0x9E 1 0 0 1 1 1 1 0
Advanced C
Operators – Bitwise – Shift

Syntax

Left Shift :
shift-expression << additive-expression
(left operand) (right operand)
Right Shift :
shift-expression >> additive-expression
(left operand) (right operand)
Advanced C
Operators – Bitwise – Left Shift

'Value' << 'Bits Count'



Value : Is shift operand on which bit shifting effect to be
applied

Bits count : By how many bit(s) the given “Value” to be shifted

Say A = 0x61 A << 2

Original value 0x61 0 1 1 0 0 0 0 1

Resultant value 0x84 1 0 0 0 0 1 0 0

Zero filling left shift


Advanced C
Operators – Bitwise – Right Shift

'Value' >> 'Bits Count'



Value : Is shift operand on which bit shifting effect to be
applied

Bits count : By how many bit(s) the given “Value” to be shifted

Say A = 0x61 A >> 2

Original value 0x61 0 1 1 0 0 0 0 1

Resultant value 0x18 0 0 0 1 1 0 0 0

Zero filling right shift


Advanced C
Operators – Bitwise – Right Shift – Signed Valued

“Signed Value' >> 'Bits Count'



Same operation as mentioned in previous slide.

But the sign bits gets propagated.

Say A = -95 A >> 2

Original value 0xA1 1 0 1 0 0 0 0 1

Resultant value 0xE8 1 1 1 0 1 0 0 0

Sign
Zero
bitfilling
fillingright
rightshift
shift
Advanced C
Operators - Bitwise
025_example.c
#include <stdio.h>

int main()
{
int count;
unsigned char iter = 0xFF;

for (count = 0; iter != 0; iter >>= 1)


{
if (iter & 01)
{
count++;
}
}

printf(“count is %d\n”, count);

return 0;
}
Advanced C
Operators – Bitwise – Shift


Each of the operands shall have integer type

The integer promotions are performed on each of the
operands

If the value of the right operand is negative or is greater
than or equal to the width of the promoted left operand,
the behavior is undefined

Left shift (<<) operator : If left operand has a signed type
and nonnegative value, and (left_operand * (2^n)) is
representable in the result type, then that is the
resulting value; otherwise, the behavior is undefined
Advanced C
Operators – Bitwise - Shift


The below example has undefined behaviour

026_example.c
#include <stdio.h>

int main()
{
int x = 7, y = 7;

x = 7 << 32;
printf(“x is %x\n”, x);

x = y << 32;
printf(“x is %x\n”, x);

return 0;
}
Advanced C
Operators – Bitwise - Mask


If you want to create the below art assuming your are not
a good painter, What would you do?
Advanced C
Operators – Bitwise - Mask


Mask the area as shown, and use a brush or a spray paint
to fill the required area!
Advanced C
Operators – Bitwise - Mask


Fill the inner region
Advanced C
Operators – Bitwise - Mask


Remove the mask tape
Advanced C
Operators – Bitwise - Mask


So masking, technically means unprotecting the required
bits of register and perform the actions like
– Set Bit
– Clear Bit
– Get Bit
etc,..
Advanced C
Operators – Bitwise - Mask

7
0 6 5 4 3 2 1 0 Bit Position

0 1 1 0 0 0 0 1 The register to be modified


& Operator
0
1 1 0 1 1 1 1 1 Mask to CLEAR 5th bit position

0 1 0 0 0 0 0 1 The result

0 1 0 0 0 0 0 1 The register to be modified


| Operator
0 0 1 0 0 0 0 0 Mask to SET 5th bit position

0 1 1 0 0 0 0 1 The result
Advanced C
Operators – Bitwise - Shift


W.A.P to count set bits in a given number

W.A.P to print bits of given number

W.A.P to swap nibbles of given number
Advanced C
Operators – Ternary
Syntax
Condition ? Expression 1 : Expression 2;
027_example.c
#include <stdio.h>

int main()
{ #include <stdio.h>
int num1 = 10;
int num2 = 20; int main()
int num3; {
int num1 = 10;
if (num1 > num2) int num2 = 20;
{ int num3;
num3 = num1;
} num3 = num1 > num2 ? num1 : num2;
else printf(“Greater num is %d\n”, num3);
{
num3 = num2; return 0;
} }
printf(“%d\n”, num3);

return 0;
}
Advanced C
Operators – Comma


The left operand of a comma operator is evaluated as a
void expression (result discarded)

Then the right operand is evaluated; the result has its
type and value

Comma acts as separator (not an operator) in following
cases -
– Arguments to function
– Lists of initializers (variable declarations)

But, can be used with parentheses as function arguments
such as -
– foo ((x = 2, x + 3)); // final value of argument is 5
Advanced C
Over and Underflow


8-bit Integral types can hold certain ranges of values

So what happens when we try to traverse this boundary?

Overflow
(127 + 1)

Underflow
(-128 - 1)
Advanced C
Overflow – Signed Numbers

Say A = +127

Original value 0x7F 0 1 1 1 1 1 1 1

Add 1 0 0 0 0 0 0 0 1

Resultant value 0x80 1 0 0 0 0 0 0 0

Sign bit
Advanced C
Underflow – Signed Numbers

Say A = -128

Original value 0x80 1 0 0 0 0 0 0 0

Add -1 1 1 1 1 1 1 1 1

Resultant value 0x7F 1 0 1 1 1 1 1 1 1

Sign bit
Spill over bit is discarded
Arrays
Advanced C
Arrays – Know the Concept

A conveyor belt

Starts here
Equally spaced
Defined length
Carry similar items
Index as 10th item
Ends here
Advanced C
Arrays – Know the Concept
Conveyor Belt An Array
Top view First Element
Start (Base) address


Total Elements

Fixed size

Contiguous Address

Elements are
accessed by
indexing

Legal access region

Last Element
End address
Advanced C
Arrays
Syntax
data_type name[SIZE];

Where SIZE represents number of elements


Memory occupied by array = (number of elements * size of an element)
= (SIZE * <size of data_type>)

Example
int age[5] = {10, 20, 30, 40, 50};

1 2 3 4 5
Index Index Index Index Index

10 20 30 40 50

base addr + 12

base addr + 16
base addr + 4

base addr + 8
base addr
Advanced C
Arrays – Points to be noted


An array is a collection of similar data type

Elements occupy consecutive memory locations (addresses)

First element with lowest address and the last element with
highest address

Elements are indexed from 0 to SIZE – 1. Example : 5
elements array (say array[5]) will be indexed from 0 to 4

Accessing out of range array elements would be “illegal
access”
– Example : Do not access elements array[-1] and array[SIZE]

Array size can't be altered at run time
Advanced C
Arrays – Why?
028_example.c
#include <stdio.h>

int main()
#include <stdio.h>
{
int num1 = 10; int main()
int num2 = 20; {
int num3 = 30; int num_array[5] = {10, 20, 30, 40, 50};
int num4 = 40; int index;
int num5 = 50;
for (index = 0; index < 5; index++)
printf(“%d\n”, num1); {
printf(“%d\n”, num2); printf(“%d\n”, num_array[index]);
printf(“%d\n”, num3); }
printf(“%d\n”, num4);
printf(“%d\n”, num5); return 0;
}
return 0;
}
Advanced C
Arrays - Reading
029_example.c
#include <stdio.h>

int main()
{
int num_array[5] = {1, 2, 3, 4, 5};
int index;

index = 0;
do
{
printf(“Index %d has Element %d\n”, index, num_array[index]);
index++;
} while (index < 5);

return 0;
}
Advanced C
Arrays - Storing
030_example.c
#include <stdio.h>

int main()
{
int num_array[5];
int index;

for (index = 0; index < 5; index++)


{
scanf(“%d”, &num_array[index]);
}

return 0;
}
Advanced C
Arrays - Initializing
031_example.c
#include <stdio.h>

int main()
{
int array1[5] = {1, 2, 3, 4, 5};
int array2[5] = {1, 2};
int array3[] = {1, 2};
int array4[]; /* Invalid */

printf(“%u\n”, sizeof(array1));
printf(“%u\n”, sizeof(array2));
printf(“%u\n”, sizeof(array3));

return 0;
}
Advanced C
Arrays – Copying


Can we copy 2 arrays? If yes how?
032_example.c
#include <stdio.h>

int main()
{
int array_org[5] = {1, 2, 3, 4, 5};
int array_bak[5];
Waow!! so simple?
int index; But can I do this

array_bak = array_org;

if (array_bak == array_org)
{
printf(“Copied\n”);
}

return 0;
}
Advanced C
Arrays – Copying


No!! its not so simple to copy two arrays as put in the
previous slide. C doesn't support it!

Then how to copy an array?

It has to be copied element by element
Advanced C
Arrays – DIY


W.A.P to find the average of elements stored in a array.
– Read value of elements from user
– For given set of values : { 13, 5, -1, 8, 17 }
– Average Result = 8.4

W.A.P to find the largest array element
– Example 100 is the largest in {5, 100, -2, 75, 42}
Advanced C
Arrays – DIY


W.A.P to compare two arrays (element by element).
– Take equal size arrays
– Arrays shall have unique values stored in random order
– Array elements shall be entered by user
– Arrays are compared “EQUAL” if there is one to one
mapping of array elements value
– Print final result “EQUAL” or “NOT EQUAL”

Example of Equal Arrays :


– A[3] = {2, -50, 17}
– B[3] = {17, 2, -50}
Advanced C
Arrays – Oops!! what is this now?

You might also like