I was doing this program from Hackerrank in C and I solved the program without declaring my character array 'result' as static. It returned an error where my Output didn't match with the Expected Output. But as soon as I inserted the keyword 'static' in the declaration of the array 'result', the code ran successfully.
Here is my code snippet:
char* timeConversion(char* s)
{
int i;
static char result[100]; // this doesn't work if i remove the word 'static'
if (s[0]=='1' && s[1]=='2' && s[8]=='A')
{
result[0]='0';
result[1]='0';
for (i=2;i<8;i++)
result[i]=s[i];
}
else if (s[0]=='1' && s[1]=='2' && s[8]=='P')
for (i=0;i<8;i++)
result[i]=s[i];
else
{
if (s[8]=='A')
for (i=0;i<8;i++)
result[i]=s[i];
if (s[8]=='P')
{
result[0]=s[0]+1;
result[1]=s[1]+2;
for (i=2;i<8;i++)
result[i]=s[i];
}
}
return result;
}
I don't understand that, what is the use of Static here?