Search code examples
cscopelocal-variables

Is it a bad practice to have local variable name same as parameter name in c?


For example, in the below code scope of local variable num should be only within else but is it a bad practice?

typedef enum
{
   FIRST,
   SECOND,
   THIRD,
} numbers;

void fun(int check, numbers *num)
{
    if (check)
    {
    ..........
    .......
    }
    else
    {
        numbers num;
        ............
    }
}

Solution

  • man gcc

    -Wshadow Warn whenever a local variable or type declaration shadows another variable, parameter, type, or class member (in C++), or whenever a built-in function is shadowed. Note that in C++, the compiler warns if a local variable shadows an explicit typedef, but not if it shadows a struct/class/enum.