ESSENTIALS OF
PROGRAMMING (C PROGRAM
AS A CASE STUDY)
CSC 101
INTRODUCTION TO COMPUTER SCIENCE
ABOUT C
• C is very concise and and user-friendly language.
• C is portable
• C is fast and efficient
• C is a middle-Level Language: This does not mean C is less powerful,
difficult or less developed than other high-level language such as BASIC
and PASCAL.
• C combines the best element of high-level language with control and
flexibility of assembly language.
C DATA TYPES & FORMAT SPECIFIERS
• Data Types Format Specifiers
• int %d
• float %f
• double %f
• char %c
• long %l
• short %i
OUTPUT IN C
int main( ){
printf(“Hello World”);
}
// This will output the text Hello World on the screen
OUTPUT
printf();
printf(“Hello World”);
The output will be
Hello World
VARIABLES
VARIABLE DECLARATION
• int x;
// The will create a memory space(container) that can hold a single value
at a time.
C OPERATORS
• Assignment Operators
• Arithmetic Operators
• Relational Operators
• Increment/Decrement Operators
• Logical Operators
ASSIGNMENT OPERATOR
In typical mathematics this means the same thing
A = 5 and B = 5
Then we can say A = B and B = A
In programming these two are entirely different
VARIABLE INITIALIZATION/ASSIGNMENT
• int x;
• x = 5;
//This will assign the value of 5 to x variable
• x = 10;
//This will change the value of x to 10;
OUTPUT
int x = 10;
int y =5;
printf(“The numbers are %d and %d”,x,y);
The output will be:
The numbers are 10 and 5
INPUT
scanf(“%d”,&x);
ARITHMETIC OPERATORS
These operators are use for calculation or mathematical computation.
According to order of precedence these operators are given as follows:
() Parenthesis
/ Division
* Multiply
% Modulus/Remainder
+ Addition
- Subtraction
RELATIONAL OPERATORS
== Equal-to or comparison
> Greater than
< Less than
>= Greater than or equals to
<= Less than or equals to
!= Not equals to
INCREMENT/DECREMENT OPERATORS
We use increment or decrement to increase or decrease the value of
variable by one.
Increment Prefix Postfix
++ ++a a++
-- --a a--
LOGICAL OPERATOR
We use it to combine more than one condition
Operator Description
&& if Ahmad is a boy AND Ahmad is <=12
years
|| if Aisha is a student OR Aisha’s level == 200
lvl
! If (4 % 2) != 0
COMPOUND ARITHMETIC OPERATOR
Arithmetic and Assignment operators are combined together to form a
new operator
Operator Description Equivalent
*= a*= constant (a = a * constant)
-= a-= constant (a = a – constant)
+= a+= constant (a = a + constant)
%= a%= constant (a = a % constant)
/= a/= constant (a = a / constant)
BASIC STRUCTURE OF C PROGRAMS
Library
Global Declarations
main() function
{
Local Declarations
Statements
Calling User Define Function
}
user_define_function
C STRUCTURE
#include<stdio.h>
int x = 10;
int main()
{
int y = 5;
}
C STRUCTURE
#include<stdio.h>
int x=2; // Global declaration
int main( ) //built-in function
{
int y =3;//local declaration
MyMax(); //calling user define function
}
int MyMax(){ //function definition
}
CONTROL STATEMENTS
• We use control statements to check for condition & take action.
If (condition)
//execute this
else
//execute this
A decision control instruction can be implemented in C using:
1. The if statement
2. The if-else statement
3. The conditional operators
IF-ELSE -EXAMPLE
If (a > b){
printf(“%d is Maximum”,a);
}
else
printf(“%d is Maximum”,b);
}
LOOP STATEMENT
We use loop in programming if we want to execute instructions
repeatedly untill certain condition is reached. Loop is also called
iteration.
THE FOR LOOP
a. The for loop
for (initialization; condition; increment){
//execute this
}
EXAMPLE
for (int i = 1; i<=5; i++){
printf(“The number is %d”,i);
}
WHILE LOOP
a. While loop
int i=1;
while (condition){
//execute this
i++;
}
EXAMPLE
while (i<=10){
printf(“%d”,i);
i++;
}
DO..WHILE LOOP-EXAMPLE
The do…while loop
do{
printf(“%d”,i);
}
while(i<=5);
POINTERS
• Pointers are variables whose values are memory addresses. Normally, a
variable directly contains a specific value. A pointer, on the other hand,
contains an address of a variable that contains a specific value.
• Pointers are variables which point to memory
PA 2002
• location
VA
ID
EXAMPLE
DECLARATION OF POINTERS…
Every Pointer has data type associated with it…
Pointers, like all variables, must be defined before they can be used. The definition
int *a;
int b;
int *number; creates a pointer number
Careful: you can not create a pointer without identifying the data type with which it
will be used.
SUMMARY OF THIS PART
• & Returns the address of the variable
• * Returns the data(value) pointed to an
address
EXAMPLE OF POINTER
main()
{
float x,y;
float *z;
y =10.0;
z=&y;
x = *z;
Printf(“%f %f\n”,y,x);
}
DETAILS ON POINTERS…
CONTENT OF N BECOME 6…
IF I SAID NUM=&N;
Note that The &( ampercent) of any variable return the address
of that variable
The & return the address of it argument
If I said num=&n means num will be assigned the value as
address of n
THE CONTENT OF NUM BECOME
16254
IF I SAID M=*NUM;
CONTENT OF M BECAME 6
*NUM HAD A VALUE OF N
NOTE:
• The indirect operator * sets the value of m to that pointed by num
n = 6;
num=&n;
m=*num;
• Has the same effect s the statement
n=6
m=n
ARRAY
RECAP ON VARIABLES
• A variable is a memory that is used to hold/store a single value that
can be used modified by the program.
ARRAY CONCEPT
Think of an Array as a Car Park
Nile University has 3 car parks with the name
Park
A Variable name
Park
B
Each of the car parks have
Park
theCmaximum cars
Size
it can take.
Each parking slot can be reference
Index
by a number
Only vehicles are allowedData type
Each parking number has locationaddress
ARRAY STRUCTURE
• The first element in every array is the zeroth
element. Thus, the first element of array C is
referred to as C[0], the second element of
array c is referred to as C[1], the seventh
element of array c is referred to as C[6], and,
in general, the ith element of array C is
referred to as C[i - 1].
• If an Array is referenced by a single index,
the Array is also known as One dimensional
Array.