Variablesjtang/archives/cs104.s14/lectures/L07... · Variables • In C, variables may be created...

Preview:

Citation preview

VariablesCMSC 104 Spring 2014, Section 02, Lecture 7

Jason Tang

Topics

• Memory Addressing• Identifiers• Declarations• Assignments

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

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

Variable Addresses

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

PI radius

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

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

C Variable Naming Conventions

• Begin name with lower case

• Separate “words” with underscores

• Examples:

• area_of_circle

• chi_square

• t_value

• Be consistent!

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

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

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;

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

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

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

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;

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

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

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;

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; }

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