Lab 8: Experiment 8

Same Identifier Used in Nested Blocks

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.

Hey, we learned our lesson from the Daves in the previous experiment, right? Ambiguity is bad thing, and what difference can some curly braces make?

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
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.