-1
I want to make a simple hangman game with some pre-determined words, right at the beginning still...
escolhendo palavra...
SIGSEGV on thread : 1685113120"
My code is this
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
int a,b=0;
char q;
char chamar(int z);
printf("escolhendo palavra...\n");
srand(time NULL);
a=(rand()%4);
q = chamar(a);
b=strlen(q);
printf("%d",b);
for(int x=0;x<=b;x++)
{
printf("_");
}
}
char chamar (int z)
{
switch(z)
{
case 0:
return "soda";
break;
case 1:
return "hair";
break;
case 2:
return "paper";
break;
case 3:
return "telephone";
break;
case 4:
return "sky";
break;
default:
return "errinho";
break;
}
}
C allows declaring
i
within thefor
? No need to declare at the start of the function?– Woss
@Andersoncarloswoss Depends on the compilation flags. If it is being compiled in C89 for example it is exactly this way and does not allow, but in more modern versions it already allows.
– Isac
Thank you very much, thank you very much.
– Calisto
Just to complement: Just like @Isac said, this syntax doesn’t work on C89 because it’s standard C99. The
i
, is usually an abbreviation of "index" ("index"). Declare outside thefor
is considered a good practice of programming because it is a syntax required from the perspective of an algorithm. This means that if you need to access the last value of the index, it will only be possible if thei
is declared outside the scope offor
because all local variables inhabit within a function, so when a function ends, all that is declared within the scope offor
is destroyed.– slayer
Remembering that "statement" and "definition" are different things.
– slayer