variablesjtang/archives/cs104.s14/lectures/l07... · variables • in c, variables may be created...

20
Variables CMSC 104 Spring 2014, Section 02, Lecture 7 Jason Tang

Upload: others

Post on 26-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

VariablesCMSC 104 Spring 2014, Section 02, Lecture 7

Jason Tang

Page 2: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Topics

• Memory Addressing• Identifiers• Declarations• Assignments

Page 3: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Memory Addresses

• Recall that a computer has billions and billions of RAM

• Unwieldy to refer to each address manually

0xABCD1200 = 3.14159 0xABCD1204 = 6 0xABCD1208 = 0xABCD1204 * 0xABCD1204 0XABCD120C = 0xABCD1200 * 0xABCD1208

Page 4: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Variables

• In C, variables may be created to give labels to specific memory addresses

• Operate similar to algebraic variables

• Assignment is from right to left

PI = 3.14159 radius = 6 radius_squared = radius * radius area = PI * radius_squared

Page 5: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Variable Addresses

• In C, every variable refers to a different memory address

PI radius

Page 6: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Variable Names

• In C, “identifiers” (i.e., variable names) may consist of letters, numbers, and underscore

• First character must be a letter or underscore

• Variable names are case sensitive

• Variables may not have same name as keywords

Page 7: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

C keywords

auto double int struct _Bool

break else long switch _Complex

case enum register typedef _Imaginary

char extern return union inline

const float short unsigned restrict

continue for signed void

default goto sizeof volatile

do if static while

Page 8: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

C Variable Naming Conventions

• Begin name with lower case

• Separate “words” with underscores

• Examples:

• area_of_circle

• chi_square

• t_value

• Be consistent!

Page 9: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

More Naming Conventions

• Constants written in all uppercase

• PI, GOLDEN_RATIO, MILES_TO_KILOMETERS

• “iterators” are single letter (more about these in a future lecture)

• i, j, k, l

Page 10: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Variables and Memory Addresses

• Recall that every variable refers to a different memory address

• The = operator copies the contents of one memory address (that a variable is referring to) to another address (that another variable is referring to)

a = 10 b = a a = 20

a has value 20b still has value 10

Page 11: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Declaring Variables

• Before using a variable, must tell compiler of its existence via a declaration

• Declaration consists of variable type and variable name

• Examples of declarations:• int length_of_rectangle;

• float MILES_TO_KILOMETERS;

Page 12: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Variable Types

• Recall that within a computer, everything is a number

• Variables grouped by value type and “precision”

Integers Floating Point

type variable size type variable size

int word (usually) float word (usually)

char byte double two words

bool bit

Page 13: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Variable Assignment

• The = operator is only for assignment

• It does not denote an equality check

• As with algebra, assignment is read “right-to-left”

int alice, bob, carol; alice = 10; bob = alice * 20; /* alice + bob = carol; */ Not legal C code

Page 14: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

What Compiler Really is Doing• Allocates space in memory for variable

• Sets variable to refer to that address

int rect_len; rect_len = 42;

rect_len

Page 15: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Declarations versus Initializations

• A declaration is something that notifies compiler of the existence of a variable• int foo; double bar; char baz;

• An initialization is something that causes the compiler to actually store a value in that thing• foo = 31337; bar = 0.5; baz = 42;

• Can combine both in a single line• int foo = 31337; double bar = 0.5;

Page 16: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Declaration and Initialization example

int main(void) { float my_grade; my_grade = 1.00; /* oops, forgot to submit hw */ my_grade = my_grade - 0.10; /* 1% extra credit bonus */ my_grade = my_grade * 1.01; return 0; }

Following declaration, memory contents are uninitialized

Here is an assignment

Page 17: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Displaying Variable Contents

• Use the printf function to display contents of variable

• printf("My grade is %f\n", my_grade);

• %f is a format specifier that means “substitute the contents of a floating-point variable here”

• Use %d to substitute an integer

• Can have multiple format specifiers at once:• printf("Alice is %d, while Bob is %d

Page 18: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Variable Standards

• Add a comment when declaring non-obvious variables

• Do not have “magic numbers”; use constants instead

/** my grade in CMSC 104 */ float my_grade;

/** penalty for missing a homework */ float HW_PENALTY = 0.10; my_grade = my_grade - HW_PENALTY;

Page 19: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Better example#include <stdio.h> int main(void) { float my_grade; my_grade = 1.00; float NO_HW_PENALTY = 0.10; my_grade = my_grade - NO_HW_PENALTY; float EC_BONUS = 1.01; my_grade = my_grade * EC_BONUS; printf("Final grade is %f\n", my_grade); return 0; }

Page 20: Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created to give labels to specific memory addresses • Operate similar to algebraic variables

Better example#include <stdio.h> int main(void) { float my_grade; my_grade = 1.00; float NO_HW_PENALTY = 0.10; my_grade = my_grade - NO_HW_PENALTY; float EC_BONUS = 1.01; my_grade = my_grade * EC_BONUS; printf("Final grade is %f\n", my_grade); return 0; }

Whenever using printf, you need this preprocessor directive

In C, statements can span across multiple lines