简体   繁体   中英

Declaring arrays inside an if statement

#include <stdio.h>

int main(){
    char x;

    printf("enter something ");
    scanf("%c",&x);

    if(x == 's') char y[] = "sauve";

    else char y[] = "hi"

    printf("%s",y);
    getch();
}

It says "y" is not declared first. I'm new to arrays. What I want to do is display the string "suave" when the user inputs letter s.

Do like:

 char *y;
 if(x == 's') y = "sauve";
 else y = "hi";
 printf("%s",y);

Otherwise you have to use strcpy() and declare array before if:

 char y[SIZE] = "";       //1. sufficiently long size
 if(x == 's') 
      strcpy(y, "sauve"); //2. after declaration you can't assign used strcpy
 else 
     strcpy(y, "hi");
 printf("%s", y);        //3. now scope is not problem 

You code translates to

#include<stdio.h>
int main() {
    char x;
    printf("enter something ");
    scanf("%c",&x);
    if(x == 's') {
        char y[] = "sauve";
    } else {
        char y[] = "hi";
    }
    printf("%s",y);
    getch();
}

As might seem more obvious now, the variable 'y' that you declare is bound to the { ... } scopes it is declared in. You cannot use 'y' outside of the scope it was declared in. To fix this, declare 'y' in the outer scope like this:

#include<stdio.h>
int main() {
    char x;
    printf("enter something ");
    scanf("%c",&x);
    const char *y;
    if(x == 's') {
        y = "sauve";
    } else {
        y = "hi";
    }
    printf("%s",y);
    getch();
    return 0;
}

Note also how I use a pointer instead of an array, because the size of the array can't be known when 'y' is defined. Also don't forget to return something from main().

Use this instead :

char *y;
if(x == 's') y = "sauve";
else y = "hi";

printf("%s",y);

By declaring y before the if statement and not inside, you are extending the y scope. And you don't need braces here.


EDIT : (From the comment of Eric and Carl)

if (x == 's') char y[] = "sauve";
else char y[] = "hi";

printf("%s",y); 

In the C grammar, a declaration is not a statement. The syntax for if is if (expression) statement [else statement] . The single “statement” in an if without braces must be a statement. It may not be a declaration . It can be a compound-statement , which is a brace-enclosed block-item-list , and block-item-list may be or contain a declaration .

So here the declaration is purely illegal. You cannot declare y in an if-statement without braces.

But if you add braces :

if (x == 's') { char y[] = "sauve"; }
else { char y[] = "hi"; }

printf("%s",y); 

Here it is legal in theory but there is a new problem now... The declaration of y is now bounded to the { ... } scope. There will be an error of the type : error: use of undeclared identifier 'y' on the printf line.

Your array declaration is in if scope not in the main function. Therefore you can't acces them. Declare it at the beginning of the main function. it is nothing special to arrays. Technically in C89, all the variables are defined at the starting of a scope, C++ allows the declaration of variable anywhere in the scope. (Thanks to the larsmans for his comments. I think many textbooks and blog entries should be updated.)

If I put in the blocks {} we get:

#include<stdio.h>
    int main(){
    char x;

    printf("enter something ");
    scanf("%c",&x);

    if(x == 's') { char y[] = "sauve";}

    else {char y[] = "hi";}

    printf("%s",y); /* y is now out of scope of both its declarations*/
    getch();
   }

Does that explain what is happening ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM