LEC 2 Flashcards

1
Q

*

A

– evaluate expression to integer

– use integer as address to get value from memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

*(&name)

A

get address associated with name

get value from memory at that address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

assignments are

A

expressions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

space allocated to declarations on ?

A

on stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

while(expression

while(expression)
statement ->

A
  1. evaluate expression
  2. if non‐zero then
    i. execute statement
    ii. repeat from 1.
  3. if zero then end iteration
    • break in statement ends enclosing iteration
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Iteration: for

for(exp1;exp2;exp3)
statement ->
exp1;
while(exp2)
{ statement
exp3;
}
A
  1. execute statement exp1
  2. repeatedly test exp2
  3. each time exp2 is true
  4. execute statement
  5. execute statement exp3
    • all exps and statement
    are optional
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Condition: if

if(expression)
statement1
else
statement2 ->

A
  1. evaluate expression
  2. if non‐zero then execute statement1
  3. if zero then execute statement2
    • else statement2 is optional
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Condition: switch

switch(expression)
{ case constant1: statements1
case constant2: statements2
...
default: statementsN
}
A
  1. evaluate expression to a value
  2. for first constanti with same value, execute statementsi
  3. if no constants match expression, evaluate default
    statementsN
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Function declaration

type name(type1 name1,...,typeN nameN)
{ declarations
statements
}
A
  1. evaluate expression to a value
  2. for first constanti with same value, execute statements
  3. if no constants match expression, evaluate default
    statementsN
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Function call

name(exp1,…,expN)

A
  1. evaluate expifrom left to right
  2. push expi values onto stack
  3. execute body, allocating stack space to any
    declarations
  4. reclaim stack space for parameters & any
    declarations
  5. return result if any
    • NB end statement function call with ;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Changing parameters

A

• parameters passed by value
• value of actual parameter copied to space for
formal parameter
• final value is not copied back from formal to
actual
• so changing the formal does not change the
actual

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Pointers

type*name;

A
• variable name holds address for byte
sequence for type
• allocates stack space for address but does not
create instance of type
• must allocate space explicitly
NULL
• empty pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to actually change parameters?

A

• to change actual parameter
– pass address (&) of actual to formal
– change indirect (*) on formal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is FILE?

A

• type for system representation of file
• all files are treated as text
– i.e. character sequences

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

FILE * name;

A

• name is the address of a FILE value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

FILE * fopen(path,mode)

A

• open file with path string
• return
– FILE address
– NULL if no file

• mode
– “r” – read
– “w” – write – create new empty file – delete old version!
– “a” – append i.e. write at end of existing file

17
Q

fclose(FILE *)

A
  • close file
  • system may not do this for you when program stops
  • for I/O, C handles characters as ints
  • EOF == system end of file constant
18
Q

int getc(FILE *)

A

• return next byte from file address
– as int
• return EOF if at end of file

19
Q

int putc(int,FILE *)

A

• puts int to file address
– as byte
• return EOF if failure

20
Q

main(int argc; char ** argv)

A

• argc == number of command line arguments
– including call to executable

• argv == pointer to pointer to char
– i.e. pointer to sequence of char
• i.e. array of strings
– argv[0] == name of executable
– argv[1] == 1st argument
– argv[2] == 2nd argument
21
Q

exit(int)

A

end program
• return value of int to outer system
• in stdlib library

22
Q

file copy

$ copy path1 path

A
  1. check correct number of arguments
  2. open file1
    – check file1 exists
  3. open file2
    – check file2 exists
  4. copy characters from file1 to file2 until EOF
  5. close files
23
Q

formatted input

A
int fscanf(FILE *,”format”,exp1,exp2...)
– expi == lvalue
24
Q

formatted output

A
int fprintf(FILE *,”format”,exp1,exp2...)
– expi == rvalue