Skip to content
EMPS intranet

Back to top
Hi!   Hi!           Start program

Try stepping through the code


  Value and type of last evaluated expression: Address of array element: Address of array elementx:  (none)

Your browser does not support the canvas element which will make some the features unavailable.

If you are using Internet Explorer within the University of Exeter try going to the Settings menu (probably the gear shape at the top right of this page), selecting "Compatibility View settings", unchecking "Display intranet sites in Compatibility View" and reloading this page.

Code

 Header...
// // Read the high score from a file, returning zero // if the file does not exist.
int readhighscore(){ » int hs; » FILE *infile; » infile=fopen("highscore.txt", "r"); » if (infile==NULL) // First time game has been played return 0; » fscanf(infile, "%d", &hs); » fclose(infile); » return hs; }
...
// // Write the high score file checking that this game's // high score is greater than the saved one. void writehighscore(int hs){ » FILE *outfile; » if (hs<=readhighscore()) return ; » outfile=fopen("highscore.txt", "w"); » if (outfile==NULL) { » fprintf(stderr, "Cannot write high-score file\n"); » exit(1); } » fprintf(outfile, "%d", hs); » fclose(outfile); }
...
int main(){ » int highscore; » highscore=readhighscore(); » printf("High score: %d\n", highscore); » writehighscore(12); » highscore=readhighscore(); » printf("High score: %d\n", highscore); » writehighscore(2);// Will do nothing » return 0; }
...

Data

readhighscore()

hs (int)   infile (FILE *)
132   136
-1583616
  
-4158853638

readhighscore()

hs (int)   infile (FILE *)
132   136
-1583616
  
-4158853638

writehighscore()

outfile (FILE *)   hs (int)
184   208
-4158853530
  
2

writehighscore()

outfile (FILE *)   hs (int)
184   208
-4158853530
  
12

readhighscore()

hs (int)   infile (FILE *)
196   200
1
  
-4158852972

readhighscore()

hs (int)   infile (FILE *)
196   200
-1583132
  
264

main()

highscore (int)
248
-1767816402

Memory

readhighscore()

132
133
134
135
hs = -1583616
136
137
138
139
infile = -4158853638

readhighscore()

132
133
134
135
hs = -1583616
136
137
138
139
infile = -4158853638

writehighscore()

184
185
186
187
188
189
190
191
outfile = -4158853530
?
?
?
?
208
209
210
211
hs = 2

writehighscore()

184
185
186
187
188
189
190
191
outfile = -4158853530
?
?
?
?
208
209
210
211
hs = 12

readhighscore()

196
197
198
199
hs = 1
200
201
202
203
infile = -4158852972

readhighscore()

196
197
198
199
hs = -1583132
200
201
202
203
infile = 264

main()

248
249
250
251
highscore = -1767816402

NB: the actual memory address of each variable is the
address shown plus 4293383472 (0xFFE7D530).

Show Advanced options

.
Show output (Before looking at the output, work out what you think it put should be and see if you are right.)

Output

High score: 0
High score: 12
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Share
Back to top