Home > other >  What is the process of redeclaration in C ?
What is the process of redeclaration in C ?

Time:10-14

A declaration (Clause 9) may introduce one or more names into a translation unit or redeclare names introduced by previous declarations. If so, the declaration specifies the interpretation and semantic properties of these names.

Does redeclaration only happens when all semantic properties are the same, or just the same name is needed?

int main() {
    int a = 12;
    {
        char a;
    }
}

In the code above, what happens in the inner rounded brackets? a declaration or a redeclaration? In the following code, is there a declaration or redeclaration?:

int main() {
    int a = 12;
    {
        int a ;
    }
}

CodePudding user response:

In both cases the inner declaration of variable a hides the outer variable a. So if you use variable a in any manner, inside the inner block then it will refer to inner a.

For example, if you modify the first case as follows:

 int a = 12;
    {
        char a;
        decltype(a) p;//p is a char type instead of int
    }

then the type of variable p is char and not int. This is because as i said when you write decltype(a) then this a inside the parenthesis refers to the inner variable a instead of the outer variable a i.e., the inner declaration of a hides the outer a. And since the inner a is of type char so p will be of type char.

Now lets modify your example 2 as follows:

int a = 12;
{
   int a  = 15;
   std::cout<<a <<std::endl;//this will print 15 
}

In this case also the inner declaration(which is also a definition) of a hides the outer a. So when we write std::cout<<a; , we are referring to the inner a which has value 15. And so we will see 15 printed on the screen. Again the inner declaration of a hides the outer a.

CodePudding user response:

Redeclarations a valid in some cases in c . Here is one example of a valid redeclaration:

// file1.c
int Count;

// file2.c
void foo(void) {
  extern int Count;
  Count  ;
}

Both of your examples are simply declarations of a new local variable that has the same name as another local variable in the outer scope. The inner local variable shadows (hides) the outer one. If you had not introduced a new scope with {} then a would have been redefined.

Both variables are alive at the same time, the lifetime of the inner one is included in the lifetime of the outer one. At the end of the inner scope, the inner variable's lifetime will be over, and the outer variable can be easily accessed again.

You can see this clearly demonstrated with the following little example:

struct A {
    A() { cout << "Object being created\n"; }
    ~A() { cout << "Object being destroyed\n"; }
};

void main() {
    A a1; 
    cout << "Beginning inner scope\n";
    {
        A a1;
    }
    cout << "End of inner scope\n";
}

In C ( and C and Java and others ) it is legal for the same name to be used within multiple scopes - some compilers (e.g. gcc -Wshadow) can generate a warning about this since it can cause confusion.

  • Related