Lecture 07
Lecture 07
PROGRAMMING (C PROGRAM
AS A CASE STUDY)
CSC 101
INTRODUCTION TO COMPUTER SCIENCE
ABOUT 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
A = 5 and B = 5
Then we can say A = B and B = A
• 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
== Equal-to or comparison
> Greater than
< Less than
>= Greater than or equals to
<= Less than or equals to
!= Not equals to
INCREMENT/DECREMENT OPERATORS
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
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>
}
CONTROL STATEMENTS
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
a. While loop
int i=1;
while (condition){
//execute this
i++;
}
EXAMPLE
while (i<=10){
printf(“%d”,i);
i++;
}
DO..WHILE LOOP-EXAMPLE
ID
EXAMPLE
DECLARATION OF POINTERS…
Pointers, like all variables, must be defined before they can be used. The definition
int *a;
int b;
Careful: you can not create a pointer without identifying the data type with which it
will be used.
SUMMARY OF THIS PART
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
n = 6;
num=&n;
m=*num;
n=6
m=n
ARRAY
RECAP ON VARIABLES