Lecture #7: Declarations

CSE 5A

Bob Pitha

bpitha@cs.ucsd.edu

http://www-cse.ucsd.edu/classes/fa99/cse5a

administrivia page 1

•      Class web page has been updated recently

–   homework #1

–   course outline

•      Homework issues

–   you can use the lab

–   you can use your home computer

–   you can use your friend’s computer

–   you can use any computer you like

•   but if it’s not the lab’s computer, it’s up to you to get and use the software

administrivia page 2

•      Homework (continued)

–   how to turn homework in (again)

•   best option: leave it in the directory I specified in the assignment

–   H:\Homework\HW1

•   second best option: e-mail the files to cs5a@ieng9.ucsd.edu

•   third best option: floppy disk

–   marked with your name and class account name

administrivia page 3

•      Quiz

–   Friday, not Wednesday, during class

–   will cover everything up through today

•   general programming concepts

•   data types

•   constants

•   declarations

declarations

•      What is an declaration?

–   how we introduce an identifier

–   specifies the interpretation and attributes of an identifier

•   in other words, tells the computer what an identifier means

–   variable
–   function
–   various other things

•   let’s focus on variables right now

variables

•      So, what is a variable?

•      Sometimes called an object

–   though less so these days, since it’s not the same as the objects in “object-oriented” programming

–   your text defines object as “a named region of storage”

–   ANSI standard:

“A region of data storage in the execution environment, the contents of which can represent values. … objects are composed of contiguous sequences of one or more bytes, the number, order, and encoding of which are either explicitly specified or implementation-defined.”

variables

•      So variables are memory regions you can use to hold data while your program is running.

•      For your convenience, you give them names (instead of having to know the address)

•      Because different types of data are different sizes, the computer needs to know what type each variable is

–   so it can reserve the memory you need

back to declarations

•      So all this means that before you can use a variable, you need to tell the compiler:

–   its name

–   its data type

–   anything else that the compiler needs to know

•   storage class

•   right now, we’ll just stick to the first two

why declare variables?

•      Easier for the compiler

–   it doesn’t have to guess what you want

–   it can reserve the right amount of memory

–   machine code can be different for some different data types

•      Discipline

–   forces you to think about what you need

–   this is a subtle issue, we’ll revisit it

declaration rules

•      Each declaration consists of:

–   a data type

•   int, double, char…

–   a list of variable names

•   remember the rules for identifiers

–   letters, digits, underscore
–   can’t start with a digit
–   case sensitive

–   optional initialization

–   ends with a semicolon

simple declaration examples

Text Box: char c;
int i;
double d;
int lower, upper, step;
 


initialization?

•      Sets the initial value of the variable

–   this doesn’t mean it can’t change subsequently

–   must be a constant expression

•   a constant

•   an expression whose value is constant

Text Box: char c = ‘x’;
int i = 37;
double d = 12.34;
int lower=0, upper=300, step=20;
 


initialization and data types

•      The initializer must be:

–   of the same data type as the variable

–   of a data type the compiler can convert into the data type of the variable

Good:

Text Box: int i = 3;
double a = 19.38;
double b = 12;

 


Less good:

Text Box: char c = 3.1415;
float f = 12.34;

 


placement of declarations

•      Placement in program is significant

–   At beginning of statement block

•   this is what we’ve seen so far

–   Outside a function - global variable

–   Parameter declaration - in function declaration

variables in statement blocks

•      What statement blocks have we seen so far?

–   after all, we haven’t yet even talked about statement blocks!

Text Box: /*
 * HelloWorld: A simple C program
 */

#include <stdio.h>

int main (void)
{
printf (“Hello, world!\n”);
return 0;
}
 


–   statement blocks are surrounded by { }

another statement block

Text Box: /*
 * FtoC1 – print a Fahrenheit-to-Celsius
 * conversion table
 */

#include <stdio.h>

int main (void)
{
   int fahr;
   int celsius;
   int lower = 0;
   int upper = 300;
   int step = 20;

   fahr = lower;
   while (fahr <= upper) {
      celsius = 5 * (fahr – 32) / 9;
      printf (“%d\t%d\n”, fahr, celsius);
      fahr = fahr + step;
   }

   return 0;
}
 


back to variables and statement blocks

•      So any statement block can start with some variable declarations

–   but those variables are only available to the code inside the statement block

•   this is a concept called scope…

global variables

•      Variables that are defined outside of any functions

•      Potentially available to all functions

–   there are some rules here

•      Analogy:

–   account slips: local variables

–   web page: global variable

function parameters

•      We haven’t talked about functions much yet

•      Parameters are data you give to a function each time it is invoked

–   remember my driving analogy of a function

•   adjust speed to X mph

•   the same instructions, no matter what X is each time

–   remember how we have used printf:

Text Box: printf (“Hello, world!\n”);
 

Text Box: printf (“i is %d\n”, i);
 

Text Box: printf (“%d\t%d\n”, fahr, celsius);
 


–   we’ll talk about parameters more when we get into functions