0% found this document useful (0 votes)
2K views

Basic Refresher, 1D Array

Uploaded by

Shambhavi K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Basic Refresher, 1D Array

Uploaded by

Shambhavi K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Basic Refresher

Number System
○ In the mathematics there are mainly 4 type of number system
○ Decimal (Base 10, 0 to 9)
○ Binary (Base 2, 0 and 1)
○ Octal (Base 8, 0 to 7)
○ Hexadecimal (Base 16, 0 to 16)

What is the purpose of learning here? When we type any letter or word, the
computer translates them into numbers since machines can only understand the
binary numbers. To understand this conversion better we will explore the number
system here.

Decimal to Binary Conversion


● Whenever a decimal number has to be converted to binary we will always use
the LCM method by dividing the number by 2.
E.g., number = 125, then binary will be

Decimal to binary

125 to binary

125 ⇒ 1011101

Binary to decimal
==> 1 * 2^0 + 0 * 2^1 + 1 * 2^2 + 1 * 2^3 + 1 * 2^4 + 1*2^5 + 1*2^6 + 0 *2^7

==> 1 + 0 + 4 + 8 + 16 + 32 + 64 + 0

==> 125
Decimal to octal
212 to octal ===> 0324

Decimal to hexadecimal
472 to hexadecimal ==> 1D8

Hexadecimal to binary :
● The maximum value of hexa is F which is 15, so to represent 15 4 bits are required.
● Therefore whenever hexa is to be converted to binary then each digit
should be converted to 4bit binary value like below:
Octal to binary :
● The maximum value of octal is 7, so to represent 7, 3 bits are required.
● Therefore whenever octal is to be converted to binary then each digit should
be converted to 3bit binary value like below:

Octal to hexadecimal:
● To convert octal number to hexadecimal
○ Convert octal number to binary
○ group binary number into 4 bits each
○ take the equivalent 4-bit value of hexa
○ E.g., 324 octal to hexa
○ 324 in binary is 011 010 100
○ Group above binary to 4 bits then it will be:

● S0, 324 octal == D4 in hexa

Hexadecimal to octal:
e.g, 1D8 in octal
Data Representation
● In a computer system different data is represented in a specific way. Here, we will
try to explore different ways of representing data in computer
● First, is Bit representation, it's the only language which is understood by the
machine.
○ Bit is derived from the word binary digit.
○ It has 2 possible states: true or false or 0 or 1.
● Byte is a collection of 8-bit.
○ its a smallest unit of information in the computer memory.
● Character is a 1byte integer which is represented differently in different languages
with the help of character code like, ASCII, EBCD, UTF etc.
○ In C, character is represented with the ASCII code.
○ Characters are represented with ‘ ‘ and a character will be 1byte
information
○ For example if you use any character internally these are represented as
follows:

'0' → 48(ASCII code) → 0011 0000

'A' → 65 → 0100 0001


● Next, word is the capacity of a processor, which defines how much information it

can fetch or process at a time.

○ For example, 8-bit MC → MC can fetch 8-bit of information at one time/one

cycle 32 --> fetching 32-bits of information at a time.

● Integer numbers are represented as a direct binary conversion

○ for example, positive 13 will be in 32 bits

0000 0000 0000 0000 0000 0000 0000 1101

● Negative number -13 will be stored as 2s complement

1's complement + 1
0000 0000 0000 0000 0000 0000 0000 1101

1111 1111 1111 1111 1111 1111 1111 0010


+0000 0000000 0000 0000 00000000 0001
1111 1111 1111 1111 1111 1111 1111 0011
● In other word -13 in 2’s complement can be in 8-bit system

==> 256 -13 ==> 255


Float Representation

● Real values in C are represented as a IEEE 754 standard format which


follows following steps

step 1: Convert fractional and integral value to binary

SO, 10.25 ⇒ 1010.010

Step 2 : Convert the result of step1 to standard exponent formula

1.Xe+/-y

X --> mantissa --> 01001000000000000000000 (23 bits in float)


sign bit - 0

step 3: Find the exponent part

--> add or subtract y with the standard bias number


--> for floating point standard bias number is --> 127
--> if y is positive add else if negative subtract
exponent = 127 + 3 == 130 ⇒

0 1 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
● If negative only is given then only sign bit will be 1 other rest of the steps are same
as previous
● If the value is double then step 1 and 2 remain the same. In step 3 the standard
bias number will be 1023 for double.

2. 1.7 --> never ending number


==> 1
.7 * 2 = 1.4
1.10110011001100110.......
.4 * 2 = 0.8
.8 * 2 = 1.6
.6 * 2 = 1.2
.2 * 2 = 0.4
.4 * 2 = 0.8
A.8 * 2 = 1.6
.6 * 2 = 1.2
.2 * 2 = 0.4

step 2: no need to convert so


--> y=0

X==> 1 0110 0110 0110 0110 0110 01


sign = 0

step 3: exponent
127 + 0 == 127==> 0111 1111

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

For double

--> All the steps are same only change is representation of exponent and mantissa and
standard bias number is 1023

--> exponent --> 11


--> mantissa --> 52 1. 2.5
10.10
2. y = 1

1.010e

X - 0100000000000000000000000000000000000000000000

sign = 0
3. exponent -->
1023 ==> 1023 + 1 ==> 1024 ⇒ 1000000000

0 01000000000 010000000000000000000000000000000000

Data types

● Data Type specifies which type of value a variable has and how much memory can
be allocated to each.

● In C data types are divided into primitive/basic and non primitive(user defined) data
types.

● In basic data types following are the division:


○ Integral
■ int : integer type and memory allocation is compiler implementation
dependent. If turboc then sizeof(int) is 2 bytes, If gcc then the
sizeof(int) is 4 bytes.
■ char: Character type is a 1byte integer data. All the characters in C
are converted to ASCII equivalent code.
○ Real
■ Float: 4bytes real value which is a single precision data type
■ Double: 4byte real value which is a double precision datatype.

● Size of datatype can be obtained with the help of sizeof operator.


○ sizeof(variable_name);
○ sizeof(data type);
○ sizeof(constant);
○ sizeof datatype;
○ sizeof returns the value in unsigned int(%u) if 32 bit system else unsigned
long int(%lu) in 64 bit system.
○ The format specifier is %zu which is a portable one.
Modifiers and Qualifiers
● Modifiers and qualifiers are the special keywords which will modify the width,
accessibility and memory scope of the variables.
● Modifiers are divided as follows:
○ size modifier: short, long, long long

short long long long

● short modifiers can be ● long modifiers can ● long long modifiers


used with only int be used either can be used with only
datatype with int or double int datatype
● It will modify the width data type ● It will modify the
of int to 2bytes in gcc. ● It will modify the width of int
● short int num; width of int and depending on the
short num; double which is compiler. Sizeof long
● format specifiers: compiler long will be 8byte/16
int - %hd implementation bytes
hexa - %hx/%hX dependant - ● long long int num;
octal - %ho for int 4/8, for long long num;
double - 12/16. ● format specifiers:
● long int num; int - %lld
long num;//int hexa - %llx/%llX
long double num; octal - %llo
● format specifiers:
int - %ld
hexa - %lx/%lX
octal - %lo
double - %Lf

○ sign modifiers: signed and unsigned


■ signed is a default modifier for the integral data type. Signed
variables are capable of holding both positive and negative values.
■ In signed, MSB is dedicated to the sign bit.
■ So, the range of the signed variable can be calculated as,
● -2(n-1) to +2(n-1)-1
● if character then the range is: -2(8-1) to + 2(8-1)-1 = -128 to +127
● The values outside the specified range then it will result in
overflow and underflow(discussed later)
example: signed char ch;

● Here, MSB(Most Significant Bit is dedicated for sign bit), if 0 its a positive number,

if 1 then negative number,

● So,

● The range will be, -128 to +127\

● Example,

○ signed char ch = 123; //binary is: 0111 1011

○ char ch = 133;

■ Here, binary of 133 = 1000 0101, MSB bit is 1

■ So, compiler will take the 2s complement of the number

0111 1010

0000 0001

0111 1011 → -123


Unsigned Variables

● Only positive values are stored in unsigned types.

● No special dedication for MSB bit.

● So, range = 0 to 2n - 1, so for unsigned char range = 0 to 255

● Example,

unsigned char ch = -13;

● Here, first negative value will be converted to 2s complement

● 0000 1101

1111 0010

00000001

1111 0011

● When u try to print the value then the output will be positive equivalent of 1111

0011 which is, 243

● Now, variables can be declared with both the signedness and size modifiers like,

○ unsigned long int;

○ long long int; // by default its signed

○ short unsigned int;


Statements
Conditional Constructs
● In C, conditional constructs are used to construct the statements depending on the
condition whether it's true or false. Its divided into 2 groups:
○ Single iterative : Here, statements will be executed only else depending on
the condition.
○ Multiple iterative: Here, statements are executed repeatedly until the certain
condition is true. Once the condition becomes false it will stop the loop.

Single Iteration:
● If and its family: If is a single iterative conditional construct which will execute the
statements only once.
syntax:
if(expression)
{
statements;
}
● Examples:
int num = 10;
if(num == 10)
{
printf(“num is equal to 10\n”);\
}
switch conditional construct

● Switch is also a single iterative condition construct which will execute the
statements once which is matching with the case label.

syntax:

switch(expression)

case label:

statements;

break;

case label:

statements;

break;

default:

statements

● Where, case label has to be integral constant or enum.


● Real constants are not allowed --> 2.5,4.5,7.8.......
● Case label should be unique
● Multiple values are not allowed in case label
● A break statement is used to break the switch cases.
● If break is not used then switch will result in fall through condition like
○ switch(expression)

case 10:

printf(“10 is selected\n”);

case 20:

printf(“20 is selected\n”);

default:

printf(“None is matching\n”);

● In the above example if the expression is having 10, then all the 3 printf is
executed.
switch vs if else
● consider the below scenario where x = 10, then execution time for if is

if (x == 100) 1 cycle
{
} 2 cycle 200nsec * 4 == 800nsec
else if(x == 90)
{
} 3 cycle 4th cycle
else if(x == 80)
{
}
else
{
}

For switch JUMP default:

case 100:
.
. .
case 90: default:
. 1cycle == 200nsec
.
case 80:
.
Multi Iteration Conditional construct:
● While loop is known as entry controlled loop where statements are executed
repeatedly until the condition is true.
● syntax:
while(expression)
{
statement;
}
● Example:
int iter = 0;
while(iter < 5)
{
printf(“Looped %d times\n”,iter);
iter++;
}
● In the above loop, iter is 0 so the condition is true it will enter the loop.
○ prints, looped 0 times then increments the iter = 1
○ continues till iter = 5
○ Once the condition is false comes out of the loop.
Interpretation:
1. whileÀàint main() int main()
{
{
int iter;
int iter;
iter = 0;
iter = 0;
while (iter < 5)
while (iter < 5)
printf("Looped %d times\n", iter);
iter++; {
printf("Looped %d times\n", iter);
return 0; }
} iter++;

return 0;
}

Output: looped 0 times

2.
compiler view

int main() int main()


{ {
int iter; iter = 6; int iter; iter = 6;
while (iter < 5); while (iter < 5)
{ {
printf("Looped %d times\n", iter); iter++; ;
} }
printf("iter: %d\n", iter); {
printf("Looped %d times\n", iter); iter++;
return 0; }
} printf("iter: %d\n", iter); return 0;

do…while loop

While vs do..while
For loop:
● For loop is also like while loop, which will execute the statements repeatedly until the condition
is true.
● For loop syntax:
for(initialisation; condition ; post evaluation)
{
statements;
}
● For loop will be executed as stated below:
1. Initialization
2. Condition - if true goto step 3 else goto step 6
3. Statements
4. Post evaluation expression
5. Goto step 2
6. Exit

1.
int main() int main()
{ {
int iter = 0;
int iter = 0; for (iter = 0 ; iter < 10; iter++ )
{
for (iter = 0 ; iter < 10; iter++ ); ;
{ }
printf("Looped %d times\n", iter); {
} printf("Looped %d times\n", iter);
return 0; }
} return 0;
}

2.
3.

Nested for loop:


Break:
● used to exit from the loop or switch case depending on the condition
● Break should be within the loop

continue statement
--> only used in multi iterative conditional construct --> do..while,while,for

--> used to skip that specific iteration of the loop


Goto Statement--> unconditional jump

int iter = 5;

goto lab1;
printf("Hello\n");

lab1:
printf("World\n");

--> generally it is avoided due to difficulty in tracing the flow

Which one is Efficient?


Operators
● Operator is a special symbol which will perform a specific task on the operand
and returns a value.
● Operators are divided on operands as below:
○ Unary : One operand - ++, - -, +, - etc
○ Binary: arithmetic, logical, bitwise etc.
○ Ternary: ?:

Unary Operators
--> used with one operand
--> highest precedence in the table

Increment and Decrement Operator


5. y = y++;

● These expression will have undefined behaviors because variable y is


incrementing assigning at the same time
● So, output cannot be predicted.
6. x =0;
printf("%d %d %d %d\n",++x, x++, x++, ++x);
● This also results in undefined behavior

● Because of post increment and pre increment in the same line


● I am supposed to get 4 2 11 but output will be different

sizeof() --> function or operator?

--> for sizeof u can pass any type of data

--> can also pass data type - char int float

--> sizeof(), sizeof var

Type Conversion

● Type conversion is a process of converting one type to another whenever


required.
Unary Conversion
Logical Operator – AND, OR, NOT

1. AND operator --> &&

2. OR Operator --> ||

3. NOT Operator --> !

Examples:
1. int a = 10,b=20;
a && b ==> 10 && 20 ==> true && true ==> true ==> 1 1

All the values are true except 0, NULL


Circuit Logic:

● Circuit logic is the way of evaluation used by compiler to optimize code

● For example if OR is used, num1 = 1, num2 = 0, then

if (++num1 || num2++)
{
//statement
}
Here, ++num1 ⇒ ++1 is true value so second is not evaluated
● Similarly if(num2++ && num1++)
Here, the first expression is false so the second is not evaluated.

Examples:
1.

2.
Compound Statements:

num1 += num2 += num3 += num4;

Bitwise Operators
● Bitwise operators work on bits of the given values.
● Bitwise operators are used whenever a particular bit/s has to be set, clear or
toggle.
Examples:
Shift Operators
1. Left Shift --> bits will be shifted to left side depending on the number of shift
2. right shift --> bits will be shifted to right side
2.

Right Shift: Unsigned right shift


Signed right shift:
Points to remember:
❖ Number of shift should be positive value, negative shift value will result in undefined
behavior (output cannot be predicted)
❖ right operand (number of bits/shift) has to be within the range of left operand
for example,
unsigned char ch = 90;
● so valid right operand values are 0 to 7
● in case of integer --> 0 to 31
● if right operand value is more than the range of bits of left operand then output
will be undefined behavior

❖ After shifting, if the result is not within the range of datatype then undefined behavior
e.g, signed char ch = 96;
ch << 4 ==> 1536 //the value is out of range

Applications of Bitwise operators:


Ternary Operator

● works with 3 operands → ?:


● syntax: condition ? expression_true : expression_false
● For some scenarios or requirement ternary is the optimization for if..else
● Example:

max = num1 > num2 ? num1 : num2;

num1 > num2 > printf(“Num1 is max\n”) : printf(“Num2 is max\n”);

● Can also have nested ternary like,


Comma operator:
● Comma operator have certain usage with for loop and in some expression, or
function calling
● It will evaluate the expression but consider or return the right most value to the
function or expression.

Here, num1=3

num1 = 6, x = 3, y = 4, z = 6
Examples:
Arrays

● Array is a collection of the same type of data.


● huge data type which can store more than one value
● used to organize the data

syntax:
datatype array_name[SIZE]; e.g,
int arr[5]; //integer array of size 5
int arr[5] = {10,20,30,40,50};

How to print an array?

● Arrays are printed with the help of loops


● Example:

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


printf("%d\n",arr[index]);
Different ways of declaring an array
Reading array from user:

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


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

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


{
printf(“%d ”,arr[index]);
}

You might also like