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...
#define HSFILE "highscore.txt" // // Read the high score from a file, returning zero // if the file does not exist.
int readhighscore(){ » int hs; » FILE *infile; » infile=fopen(HSFILE, "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(HSFILE, "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
-1875008
  
-4158562246

readhighscore()

hs (int)   infile (FILE *)
132   136
-1875008
  
-4158562246

writehighscore()

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

writehighscore()

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

readhighscore()

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

readhighscore()

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

main()

highscore (int)
248
-1767730386

Memory

readhighscore()

132
133
134
135
hs = -1875008
136
137
138
139
infile = -4158562246

readhighscore()

132
133
134
135
hs = -1875008
136
137
138
139
infile = -4158562246

writehighscore()

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

writehighscore()

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

readhighscore()

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

readhighscore()

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

main()

248
249
250
251
highscore = -1767730386

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

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