Tuesday 20 November 2012

Son of C Test Redux

Yesterday, the tutor of the C course went through the questions with us, as they were a little harder than last time. One of them was really tricky (and I didn't get it!):

1
2
3
4
5
#define MAX_COUNT 20
int counter;

for(counter = 0; counter < MAX_COUNT; counter++);
  printf("The value of the counter is %d\n", counter);

What's the output? (Answer in the comments)

2 comments:

  1. There should only be one line:
    The value of the counter is 19

    This is because there's a semi-colon at the end of the "for" statement, terminating it prior to the printf. See, tricky!

    ReplyDelete
  2. Sorry, that should be "The value of the counter is 20", not 19! See how easy it is!

    ReplyDelete