Reputation: 357
#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.
Upvotes: 2
Views: 5542
Reputation: 14510
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
isif (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.
Upvotes: 2
Reputation: 2256
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.)
Upvotes: -2
Reputation: 58271
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
Upvotes: 4
Reputation: 4677
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().
Upvotes: 1