Home > Software engineering >  declaration shadows a local variable in c
declaration shadows a local variable in c

Time:09-24

I am using the cs50 library and ide and when trying to compile this program I get errors saying that the variable 'answer' has already been declared and I don't know how to solve this any answers would be appreciated I am really stuck on this as I have just began to learn how to program the error log is:

char.c:9:14: error: declaration shadows a local variable [-Werror,-Wshadow]
        char answer = get_char("are you okay")

char.c:6:10: note: previous declaration is here
    char answer;

char.c:20:12: error: variable 'answer' is uninitialized when used here [-Werror,-Wuninitialized]
    while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N');

char.c:6:16: note: initialize the variable 'answer' to silence this warning
    char answer;

The code is:

#include <cs50.h>
#include <stdio.h>

int main(void)
{   
    char answer;
    do
    {
        char answer = get_char("are you okay Y/N ");
        if (answer == 'y' || answer == 'Y')
        {
            printf("you are okay");
        }
            else if (answer == 'n' || answer == 'N')
        {
            printf("you are not okay ");
        }

    }
    while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N');
}

CodePudding user response:

Note, the warning messages in your post are very helpful. Do not be shy about simply following their suggestions in-order one-by-one. (They are what I used to make the suggestions below.)

The shadow is caused by the second declaration of answer inside the do block:

char answer;// original declaration (the shadow)
do
{
    char answer = get_char("are you okay Y/N ");//2nd declaration (being shadowed)

To Fix, do this

char answer;// leave this declaration to allow proper scope
do
{
    answer = get_char("are you okay Y/N ");// uses same instance of 'answer' (no shadowing)
^^^^ removed second instance of `char`

There is a C specific discussion on variable shadowing here, and another more broad discussion here

CodePudding user response:

try removing the char in

char answer = get_char("are you okay Y/N ");

as it is declared outside the loop

and initilize it to char answer="";

CodePudding user response:

Within the compound statement of the do-while loop you need to use the same variable answer that is declared before the loop.

char answer;
do
{
    answer = get_char("are you okay Y/N ");
    if (answer == 'y' || answer == 'Y')
    {
        printf("you are okay");
    }
        else if (answer == 'n' || answer == 'N')
    {
        printf("you are not okay ");
    }

}
while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N');

Otherwise this declaration within the compound statement of the do while loop

    char answer = get_char("are you okay Y/N ");

hides the declaration of the variable with the same name before the while loop and moreover this variable is not alive outside the loop.

Pay attention to that the do-while loop is defined in C the following way

do statement while ( expression ) ;

For example you may write

do ; while ( expression );

where a sub-statement of the do-while statement is the null statement ;.

If you use a compound statement as the sub-statement like

do { /*...*/ } while ( expression );

then this compound statement forms its one scope and all variables with automatic storage duration are not alive outside the compound statement.

Also instead of call printf like this

printf("you are okay");

it is better to use a call of puts like

puts("you are okay");

because the call of puts also appends an output of the new line character '\n'.

  • Related