0% found this document useful (0 votes)
5 views20 pages

Lesson Note

The document outlines the course 'Introduction to Computer Programming I (C)' at the University of Amoud, detailing its structure, content, and key concepts in C programming. It covers the history of the C language, its importance, basic programming constructs, data types, operators, control statements, loops, and arrays. The course emphasizes practical programming skills through examples and exercises over a duration of 15 weeks.

Uploaded by

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

Lesson Note

The document outlines the course 'Introduction to Computer Programming I (C)' at the University of Amoud, detailing its structure, content, and key concepts in C programming. It covers the history of the C language, its importance, basic programming constructs, data types, operators, control statements, loops, and arrays. The course emphasizes practical programming skills through examples and exercises over a duration of 15 weeks.

Uploaded by

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

AMOUD FACULTY OF

COMPUTING
UNIVERSITY AND
INFORMATICS

Course Code: BBIT 6226

Course Title: Introduction to computer programming I (C)

Contact Hours: 60 Credit Hours: 4 Duration: 15 Weeks

Facilitator: Mr. Abdihakim A. Abdilahi, 0634579109,


[email protected]

Lesson preparation note

Chapter One: Introduction to C programming language

 History of C language

• C is a procedural programming language.

• It was initially developed by Dennis Ritchie in the year 1972.

• It was mainly developed as a system programming language to


write an operating system.

• The main features of the C language include low-level memory


access, a simple set of keywords, and a clean style, these features
make C language suitable for system programming like an
operating system or compiler development.

• Many later languages have borrowed syntax/features directly or


indirectly from the C language.

 Why learning C is important


o Learning C programming has lot of benefits.
o Learning C has a similar benefit. If the person had learned
driving on a manual car, he could have easily driven the
automatic car as well. Similarly, if a person learns C
programming first, it will help him to learn any modern
programming language as well.
o As learning C help to understand a lot of underlying architecture
of operating system. Like, pointers, working with memory
locations etc.
o ‘C’ language is widely used in embedded systems.
o It is usd for developing system applications.
o It is widely used for developing desktop applications.
o Most of the applications by Adobe are developed using ‘C’
programming language.
o It is used for developing browsers and their extensions.
o It is used to develop databases.
 IDE Installation
o An integrated development environment (IDE) is a software
application that provides comprehensive facilities to computer
programmers for software development.
o Tools provided by an IDE include a text editor, a project editor, a
tool bar, and an output viewer.
o In this course we will use an IDE called DEV-C++
 First Program

#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

 Variables and Data type in C


o A variable is reserved memory location to store values. In
other words, a variable in a C program give data to the
computer for processing.
o Every value in C has data type.
o Data type Data type is an attribute associated with a piece of
data that tells a computer system how to interpret its value
o Some common data types include integers, floating point
numbers, characters, strings, and arrays
o Variables can be declared by any name or even alphabets.
 Rules of Variables
o A variable name must start with letter or the underscore
character.
o A variable name cannot start with a number.
o A variable name can only contain alphanumeric characters an
underscores (A-Z, 0-9, _)
o Variable name are case-sensitive (age, Age, AGE are three
different variables).
 Examples of Data type
o In C programming, data types are declarations for variables.
This determines the type and size of data associated with
variables.
o Int (integers): are whole numbers that can have both zero,
positive and negative values but no decimal values.
o Float and double: are used to hold real numbers.
o The size of float is 4 bytes and the size of double is 8 bytes.
o Char (Character): is used for declaring character type
variables.
o The size of the character variable is 1 byte.
o Long: is used for declaring large numbers.
o Short: is used for declaring small numbers.
 Declaring & Initializing C Variable
o Variables should be declared in the C program before to use.
o Variable initialization means assigning a value to the variable.

Data_Type Variable_Name;
Example: Int x;

Declaring variable and giving it


values

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

Example of Arithmetic Operators

#include <stdio.h>
int main()
{
Output
int a = 9,b = 4, c;
a+b = 13
c = a+b; a-b = 5

printf("a+b = %d \n",c); a*b = 36


a/b = 2
c = a-b;
Remainder = 1
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder = %d \n",c);

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;

 Assignment Operators in C programming Language


o An assignment operator is used for assigning a value to a
variable.
o he most common assignment operator is =.
o And here are some assignment operators
 += means a = a+b
 -= means a = a-b
 /= means a = a/b
 Relational Operators in C programming Language
o A relational operator checks the relationship between two
operands.
o If the relation is true, it returns 1; if the relation is false, it returns
value 0.
o Relational operators are used in decision making and loops.
 == equal to
 > greater than
 < less than
 != not equal to
 >= greater than or equal to
 <= less than or equal to
 Logical Operators
o Logical operators are used to evaluate two or more
conditions.
o An expression containing logical operator returns either 0 or 1
depending upon whether expression results true or false.
o Logical operators are commonly used in decision making in C
programming.
 Logical AND (&&) returns true only if all operands are
true
 Logical OR (||) returns true only if one operands is true
 Logical NOT (!) returns true only if the operand is 0
 Examples of Relational and Logical Operators

Relational Operators Output
#include <stdio.h>

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

Logical Operators Output


#include <stdio.h>

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

 Loops in C programming language is a conditional concept used for


consecutively executing a line or a block of code over and over again
until it reaches the value desired by the programmer.
 Loops in C can also be combined with other control statements that
include Break statement, Goto statement and Control statement.
These loops can be used anywhere in the program, either as entry
control or exit control units.
 In C programming, there are three types of loops, namely For Loop,
While Loop and Do While Loop.
o While loop
o Do while loop
o For loop
 While loop
o In this, the condition is evaluated before processing the body
of the loop. If the condition is true, then only the body of the
loop is executed.
o Then the control goes back to the beginning after completing
the loop once. The statements in the loop will be executed
again, and if the condition is true and checked, then this
process goes on until the condition becomes false.
o If the condition is false, the control will go out of the loop.
After completion of the loop, the control will go to the
statement that is immediately after the loop, and the body can
contain more than one statement.
o The curly braces are not that important if it contains only one
statement.
o If the condition is not true in the while loop, then loop
statements won’t get executed.
 Do while loop
o In this loop, the statements in the loop need to be executed at
least once.
o After that, it checks the condition. If the condition is true, then
it will again have executed the loop; otherwise, it will exit the
loop. It is known as an exit controlled loop.
o It is similar to a while loop, and in this, the condition is always
executed after the body of the loop.
o The while loop is executed only when the condition is true, but
sometimes the statement needs to be executed at least once,
so for that do-while loop has to be used.

o The difference between while and do-while loop is that in while


loop while is written in the beginning and in do-while, the
condition is mentioned at the end, and it ends with a semicolon
(;).
 For loop
o It executes the set of statements until the time a particular
condition is accomplished.
o It is known as the Open-ended loop.
o In For loop, we can have more than one initialization or
increment/decrement, separated using a comma operator and
one condition as well.
o For loop is used to evaluate the initialization part first, and
then it checks the condition for true or false.
o If the condition is true, then it executes the set of statements
of for loop. After that, it evaluates the increment or decrement
condition until the condition becomes false, it repeats the
same steps. It will exit the loop when the condition is false.

Chapter Six and Seven: Functions and Array in C


programming language
 Array Functions in C is a type of data structure that holds multiple
elements of the same data type.
 The size of an array is fixed and the elements are collected in a
sequential manner.
 There can be different dimensions of arrays and C programming
does not limit the number of dimensions in an Array.

Different Functions of Array in C

 There are different functions that can be performed on arrays.


 Traversing an Array
o Traversing an Array means going through each element of an
Array exactly once.
o We start from the first element and go to the last element.

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

Chapter Eight: Pointer in C programming language


 The pointer in C language is a variable which stores the address of
another variable.
 This variable can be of type int, char, array, function, or any other
pointer. The size of the pointer depends on the architecture.
However, in 32-bit architecture the size of a pointer is 2 byte.
 Declaring Pointer
o The pointer in c language can be declared using * (asterisk
symbol). It is also known as indirection pointer used to
dereference a pointer.
 How to Use a Pointer
o There are a few important operations, which we will do with
the help of pointers very frequently.
o We define a pointer variable.
o Assign the address of a variable to a pointer.
o Finally access the value at the address available in the pointer
variable. This is done by using unary operator * that returns
the value of the variable located at the address specified by its
operand.
 Null Pointers
o It is always a good practice to assign a NULL value to a pointer
variable in case you do not have an exact address to be
assigned.
o This is done at the time of variable declaration. A pointer that
is assigned NULL is called a null pointer.
o The NULL pointer is a constant with a value of zero defined in
several standard libraries.
o In most of the operating systems, programs are not permitted
to access memory at address 0 because that memory is
reserved by the operating system. However, the memory
address 0 has special significance; it signals that the pointer is
not intended to point to an accessible memory location. But by
convention, if a pointer contains the null (zero) value, it is
assumed to point to nothing.

You might also like