Skip to content
EMPS intranet

Back to top
Hi!   Hi!           Start main()

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
-158304
  
-4160271830

readhighscore()

hs (int)   infile (FILE *)
132   136
-158304
  
-4160271830

writehighscore()

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

writehighscore()

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

readhighscore()

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

readhighscore()

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

main()

highscore (int)
248
1

Memory

readhighscore()

132
133
134
135
hs = -158304
136
137
138
139
infile = -4160271830

readhighscore()

132
133
134
135
hs = -158304
136
137
138
139
infile = -4160271830

writehighscore()

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

writehighscore()

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

readhighscore()

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

readhighscore()

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

main()

248
249
250
251
highscore = 1

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

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