GCC-Ubuntu: How come this code works?

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main(void)
{
	int i[0];
	i[0]=9;
	printf("%i\n", i[0]);
	return 0;
}


This code compiles as well as runs correctly on gcc. How come?
Please help! Thank you dears!
> This code compiles

Does it?
source.c:5:8: error: ISO C forbids zero-size array 'i' [-pedantic]

http://liveworkspace.org/code/2uZeub$0

It is not valid C++11 either.
I'm using GCC 4.6.3
And it compiles with it :(
Demand conformance with the standard, and gcc would generate an error.
> g++ -std=c89 -pedantic-errors -c my_ill_formed_program.c
Change int i[0] to int i[1] and the code will work :)

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main(void)
{
	int i[1];
	i[0]=9;
	printf("%i\n", i[0]);
	return 0;
}
Last edited on
Topic archived. No new replies allowed.