Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

BITS Pilani

K K Birla Goa Campus

CSF111
Computer Programming

Basics: Types, Variables, Expressions, Statements


Data Types

A name given to a set of values and


operations that can be performed on those values

Expression Value Type More examples

12 + 4 16 int -13, 42, 100000, 0

1.2 * 0.4 0.48 double 0.1, -5.4, 3.2e7

‘a’ ‘a’ char ‘Z’, ‘?’, ‘0’

“BITS” “BITS” Character “Hakuna Matata”,


string* “Z”, “What?”

These are the only types we care about for now


Memory Model 1

● What happens when an expression, e.g., 12 + 4, is processed?

● Short answer:

○ The operator + is applied to its operands 12 and 4

○ The resulting value is held somewhere

● Where is somewhere?
A peek into the memory

● Memory –human or computer– has two basic functions: Store and Retrieve

Names, faces, events, Groups of bits


Poems, anecdotes, … with varying sizes

Store and retrieve? Store and retrieve?


Fairly complex Simple
Known to have triggers Via indexes that we can name
A peek into the memory

● What happens when an expression, e.g., 12 + 4, is processed?

● Short answer:

○ The operator + is applied to its operands 12 and 4

○ The resulting value is held somewhere in a memory location

○ That can be accessed via its address or name


A peek into the memory

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!

We never used the 328

#include <stdio.h> actual address!


Address: 1004
Name: class_strength

int main() { I need a box big enough to put an


int class_strength; int in it and I want to call it
class_strength
class_strength = 328;
printf("%d", class_strength); And I want to put the value 328
in it
return 0;
} Assignment operator (=)
Evaluates the expression on the right, I want to print the integer in the
and puts the result in the location box named class_strength
named on the left
Accessing a memory location - give it a name!

328

#include <stdio.h> Address: 1004


Name: class_strength

int main() {
int class_strength; Declaration
int class_strength = 328;
class_strength = 328;
printf("%d", class_strength);
Initialisation

return 0;
}

Best practices C LANGUAGE RULE


Combine declaration and initialisation A variable must be declared before it
when possible is used
328
Here, class_strength is a
Variables variable of type int and has the Address: 1004
value 328 Name: class_strength

● A variable is the name of a location that stores a value of a particular type


○ We might say the variable “has” that value
○ We might say the variable “has” that type or “is of” that type
Naming convention: variables are all
lowercase with underscores for word
boundaries

Type: int Type: double Type: char Type: char string

328 1.414 ‘y’ “CS F111”

Name: class_strength Name: sqroot Name: response Name: name


Program variable v. Math variable

● A mathematical variable stands for an arbitrary but fixed value


○ 3x + 7 = 10 → x has a fixed value which you find by solving the
equation.
● A program variable has a particular value at any one time during program
execution, and that value (generally) may change at other times
○ Usually by using an assignment operator
○ x = 3; → overwrites x’s old value with 3
Naming convention: constants are all
Constants uppercase with underscores for word
boundaries

● A variable whose value is initialised and never changes

const int LUCKY_NUMBER = 72;


72

LUCKY_NUMBER

The keyword constBest


makes
practices
it a
Must be initialised
constant
Use constants when possible

Trying to assign it another value


LUCKY_NUMBER = 31; later causes an error
Operators, Expressions, and
Statements
Operators

● An operator is a symbol (or combination of symbols) that is used with


variables and values to simplify how you write certain program expressions
○ Usually, operators are designed to mimic mathematical notation —but
do not be fooled into confusing programming and mathematics!
Some common operators

Be careful with equality checks


int double on doubles
char

+, -, /, * +, -, /, *
Remainder operator doesn’t
% make sense on doubles

++, -- ++, --

<, <= <


< All of these>work on char too, but
>, >=
> don’t use them
==, != ==, != unless there’s no
better way to achieve what you
want to do
Expressions It has a value of some type

● An expression is a “syntactically well-formed and meaningful fragment”


(roughly analogous to a word in natural language)

class_strength * num_sections
area < 100

circumference / PI ‘a’
2+3

class_strength
Statements

● A statement is a “smallest complete unit of execution” (roughly analogous


to a sentence in natural language)
● A simple statement is terminated with a semicolon ‘;'

class_strength = 262;
printf(“BITS”);

diameter = circumference / PI;

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 = __

int height = 12;


width = __
height = __

int area_rect = width * height;

width = __
height = __
area_rect = __
Program Line State

const double PI = 3.14;

PI = __

double radius = 7.5;


PI = __
radius = __

double area_circle
= PI * radius * radius;
PI = __
radius = __
area_circle = __
Errors - three types

Syntax errors Runtime errors Logical errors

Violation of language No grammar rules are


Program compiles and
grammar rules. broken, but things that go
runs without crashing.
wrong at the time of
But, does not work as
Caught by the compiler. execution
expected.
Never goes to execution.
E.g., division by zero →
E.g., subtract instead of
E.g., Missing semicolon, undefined, so program
add.
Misspelled words, etc. crashes (some languages).

Caught early Harder to fix


Input Output
Type Placeholder

Format specifiers int %d

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)

Area of a circle with radius 5.200000 cm is 84.948594 sq-cm.


User input: using scanf

Prompt: not needed but


int main() { recommended
double radius = 5.2;
const double PI = 3.14159;
I want to take a real number from
printf("Enter radius: "); the user and put it in a box
scanf("%lf", &radius); named radius

double area = PI * radius * radius;


printf("Area of a circle with radius %lf cm is %lf sq-cm.",
radius, area);

return 0;
}

You might also like