Lesson Note
Lesson Note
COMPUTING
UNIVERSITY AND
INFORMATICS
History of C language
#include<stdio.h>
int main()
{
printf("Hello BBIT-C");
return 0;
}
o #include <stdio.h> This command includes standard
input output header file(stdio.h) from the C library before
compiling a C program.
o int main() It is the main function from where C program
execution begins.
o {} Indicates the beginning and the end of the main
function.
o printf("Hello BBIT-C"); This command prints the output
on the screen.
o return 0; This command is used to terminate a C program
(main function) and it returns 0.
Chapter Two: Working with Variables and Data types
Data_Type Variable_Name;
Example: Int x;
int x; int x = 4;
x = 4;
User Input/Output
o In this section, you will learn to use scanf() function to take
input from the user, and printf() function to display output to
the user.
o Prinft() is library function to send formatted output to the
screen.
o Scanf() is one of the commonly used function to take input
from the user.
o Here we have used %d format specified inside the scanf()
function to take int input from the user.
o Also we use &x inside scanf() because &x gets the address of x
and the value entered by the user is stored in that address.
o String is a sequence of characters terminated with a null
character \0.
o char name[10] = "Mohamed";
o in the above example the string of Mohamed contains 7
characters
#include<stdio.h>
int main()
{
char name[10];
printf("What is your name? ");
scanf("%s",&name);
printf("your name is %s ",name);
Chapter Three: Operations in C programming Language
Introduction to Operations
o An operator is a symbol that operates on a value or variable. For
example +,-, =, <, >
o C programming language has a wide range of operators to
perform various operations
Arithmetic Operators
Increment and decrement operators
Assignment operators
Relational Operators
Logical Operators
Bitwise Operators
o Arithmetic Operators
An arithmetic operator performs mathematical
operations such as addition, subtraction, multiplication,
division etc on numerical values (constants and
variables).
Additional
Subtraction
Multiplication
Division
Remainder (modulo division)
o Increment and Decrement Operators
C programming has two operators increment (++) and
decrement (--) to change the value of an operand
(constant or variable) by 1.
Increment increases the value by 1.
Decrement decreases the value by 1.
These two operators are unary operators, meaning they
only operate on a single operand.
Examples of Arithmetic operators, Increment and Decrement
Operators
#include <stdio.h>
int main()
{
Output
int a = 9,b = 4, c;
a+b = 13
c = a+b; a-b = 5
return 0;
}
Examples of Incrementing and Decrementing in C programming
language
Increment Output
#include <stdio.h>
int main()
{
int a = 4;
printf("Incremnet: %d",+ Increment: 5
+a);
return 0;
}
Decrement Output
#include <stdio.h>
int main()
int a = 4; Decrement: 3
printf("Incremnet: %d",--a);
return 0;
int main()
{int a = 5, b = 5, c = 6; A is equal to B is 1
printf("A is equal to B is %d\n",a==b);
A is greater than C is 0
printf("A is greater than C is %d",a>=c);
int main()
{ (a == b) and (c > b) is 0
int a = 5, b = 5, c = 10, result;
(a == b) and (c < b) is 1
result = (a == b) && (b > c);
Chapter Four: Decision making or Control Statements
Decision Making
o There come situations in real life when we need to make some
decisions and based on these decisions, we decide what we
should do next.
o Similar situations arise in programming also where we need to
make some decisions and based on these decisions we will
execute the next block of code.
o For example, in C if x occurs then execute y else execute z.
o Decision-making statements in programming languages decide
the direction of the flow of program execution.
o Decision-making statements available in C are:-
If statement.
If…else statement
Nested if statement
Switch
Nested switch
Jump statement (break, continue, goto and return).
If Statements
o if statement is the most simple decision-making statement.
o It is used to decide whether a certain statement or block of
statements will be executed or not.
o if a certain condition is true then a block of statement is
executed otherwise not.
Flowchart of If statements
If … else Statements
o The if statement alone tells us that if a condition is true it will
execute a block of statements and if the condition is false it
won’t.
o But what if we want to do something else if the condition is
false.
o We can use the else statement with if statement to execute a
block of code when the condition is false.
Flowchart of If … else Statements
Nested if Statements
o A nested if in C is an if statement that is the target of another
if statement.
o Nested if statements mean an if statement inside another if
statement.
o we can place an if statement inside another if statement.
Flowchart of Nested if Statements
If – else – if ladder Statement
o Here, a user can decide among multiple options.
o The C if statements are executed from the top to down.
o As soon as one of the conditions controlling the if is true, the
statement associated with that if is executed, and the rest of
the C else-if ladder is bypassed.
o If none of the conditions are true, then the final else statement
will be executed.
If – else – if ladder Flowchart
Chapter Five: Loops
Searching
o The search operation is used to find a particular data item or
element in an Array.
o We can perform searching in an unsorted array with the help of
traversal of the Array.
o The linear traversal from the first element to the last element
can be used to search if a given number is present in an Array
and can also be used to find its position if present.
o This is done by comparing each element with the given
element (which is to be searched).
o Once the element is found, the search operation is stopped.
Insertion
o Insertion operation is used to add a new element in the Array.
o When we specify the particular element and position where it
is to be added in the Array, we perform insertion operation.
However, the size of the Array is not disturbed while
performing this operation.
o An element will be inserted in an array only if it has sufficient
space to add it. If the size of an array is full already, a new
element cannot be added.
Deletion and Sorting an Array
o In delete operation, the element which already exists in the
Array is searched (using linear search) and deleted, followed
by the shifting of elements.
o The user enters the position of the element which is to be
deleted from the array.
o Deletion operation, just like the insertion operation, does not
affect the size of array. Also, the position of the element to be
deleted should be within the size of array, since the deletion of
an element beyond the size of Array is not possible.
o C program to show delete operation in an unsorted array.
o This operation is performed to sort an Array into a fixed order,
i.e., either ascending or descending.
Different ways of Sorting an Array
Bubble Sort
o Bubble sort compares all the elements one by one and sorts
them based on their values.
o It starts by comparing the first element with the second, if the
first element is greater than the second element, it will swap
both the elements, and carry on comparing the second and the
third element, and so on.
Selection Sort
o The basic idea behind selection sort is finding the least
element in the unsorted array, replacing it with the first
element.
o Then continue the same process with the rest of the unsorted
array, i.e., from the second position, then from the third and so
on.
Merge Sort
o This method of sorting is based on the divide and conquer
technique.
o It splits the array into two equal sub arrays and continues until
each sub array contains a single element, and then merges
them in a sorted manner resulting in a sorted array.
Insertion Sort
o In insertion sort, we start with the second element.
o The array elements are compared with each other in a
sequential manner. The current element (the value to be
sorted) is compared with all the elements in the sorted
subarray.
o All the elements in the sorted subarray which are greater than
the current element are shifted, and the current value is
inserted.