Lab 8: Experiment 8

Same Identifier Used in Nested Blocks

In the previous experiment we saw that the same identifier cannot be used for different purposes within the same block. However, we saw in Experiment 3 that the same identifier can be used in two different functions. So sometimes it's okay to use the same identifier for two different things and other times it isn't. The next few experiments will continue our investigation of when it is allowed and when it isn't.

Issue: What happens if we declare an identifier in a block, and then redeclare that same identifier in a block nested within that block?

Hypothesis: The compiler will not accept the same identifier declared in two nested blocks, one inside the other.

Experiment: We can answer this question by surrounding our char declaration of arg1 with braces:

int main()
  int arg1;
  arg1 = -1;
    char arg1 = 'A';
    cout << arg1 << endl;

Observation: Compile and execute your program.

Question #8.8.1: Does it compile? If not, what error message does it produce? If it does, what does the program output?


Question #8.8.2: Is our hypothesis correct? How do you know? If it's not correct, what should it be?

Back to the Lab Exercise  |  Forward to the Next Experiment

Report errors to Larry Nyhoff (