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...
#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
-4747280
  
-4155682854

readhighscore()

hs (int)   infile (FILE *)
132   136
-4747280
  
-4155682854

writehighscore()

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

writehighscore()

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

readhighscore()

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

readhighscore()

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

main()

highscore (int)
248
1

Memory

readhighscore()

132
133
134
135
hs = -4747280
136
137
138
139
infile = -4155682854

readhighscore()

132
133
134
135
hs = -4747280
136
137
138
139
infile = -4155682854

writehighscore()

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

writehighscore()

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

readhighscore()

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

readhighscore()

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

main()

248
249
250
251
highscore = 1

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

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