Professional Documents
Culture Documents
CP 02 Basics
CP 02 Basics
CSF111
Computer Programming
● Short answer:
● Where is somewhere?
A peek into the memory
● Memory –human or computer– has two basic functions: Store and Retrieve
● Short answer:
12 + 4 = 16
#include <stdio.h>
Address: 1004
int main() {
printf("%d", 12 + 4);
return 0;
If we knew this value is at
} address 1004, we could access it
later!
Variables and constants
Accessing a memory location - give it a name!
328
int main() {
int class_strength; Declaration
int class_strength = 328;
class_strength = 328;
printf("%d", class_strength);
Initialisation
return 0;
}
LUCKY_NUMBER
+, -, /, * +, -, /, *
Remainder operator doesn’t
% make sense on doubles
++, -- ++, --
class_strength * num_sections
area < 100
circumference / PI ‘a’
2+3
class_strength
Statements
class_strength = 262;
printf(“BITS”);
return 0;
This ‘=’ is an assignment
This ‘=’ is a math equality,
operator in
Program C, LHS must always
Line State
switching LHS and RHS is fine
be a variable*
int width;
width = __
width = 10;
width = __
width = __
height = __
area_rect = __
Program Line State
PI = __
double area_circle
= PI * radius * radius;
PI = __
radius = __
area_circle = __
Errors - three types
double %lf
char %c
int main() {
double radius = 5.2;
const double PI = 3.14159;
double area = PI * radius * radius;
printf("Area of a circle with radius %lf cm is %lf sq-cm”,
radius, area);
return 0;
} What do you do about trailing
zeros? (HW)
return 0;
}