predict the output

Predict output of the following program:

void func()
{
int a[1];
a[2]+=17;
}

int main(void)
{
char s[] = "hello\n";
func();
printf("%s",s);
return 0;
}

run program in linux :

i tried..but no output

Looks like homework to me.

Program prints hello. What were u expecting ?

This is not portable code. Use it exactly as given, do not correct the code in any way. You must use Linux running on an IA-32 architecture. Compile it with gcc, like this:
gcc bogus.c -o bogus
and then run it like this:
./bogus

I have copied it as it is, complied as described, but still the same.

What is the output on your side ?

"int a[1];
a[2]+=17;"

Its clearly violating the basic rules of array declaration...
But again, wats the purpose of this question???

abey.

Might be an interview question ...

The program might do anything from print "Hello world" to core dump (or GPF or whatever).

You won't really know until you compile it or run it and even then the result may be different depending on all sorts of factors e.g. compiler used and Operating System. And worse, it may not run in the same way twice either.

Why? Because a[2] is not declared. The syntax is valid so the C compiler will produce an executable that will reference the memory location &a+2, but that access is not legitimate. You could be changing another, unrelated variable or accessing the code section of the process, or the stack or the heap. You may even be accessing an memory area outside of your program's allotted segment.

On a small program you may not experience any problems at all, but as the program becomes larger and more complex the likelihood in encountering problems increases. Your program may not out and out crash, but may become dysfunctional for no reason that can be explained. Or unpredicatable, sometimes referred to as 'unstable'.

C is a powerful language precisely because it allows these freedoms with memory access, but bad memory management in your program is the primary risk with C too!

In the 2nd post in this thread, vivek_damodaran mentioned the result that the author of this program was seeking. Excuse me now...whenever I post in this thread I need to go wash my hands off.