While studying static
qualifier in C
, I wrote the following code by mistake.
I thought that getEven()
function would not be compiled but it works well.
why can i declare a variable without type?
I tried some test and found that the type of static
variable without type is 4
byte integer.
//This code works well.
int getEven(int i) {
static int counter = 0;
if (i%2==0) {
counter++;
}
return counter;
}
//I thought this code would make compile error, but it also works well.
int getEven_(int i) {
static counter = 0; //No type!
if (i % 2 == 0) {
counter++;
}
return counter;
}
int
by default. Modern versions of C disallow this and always require an explicit type. See here: stackoverflow.com/a/8220672/159145int
) was removed from C99, but many compilers continued to allow it for reasons of backwards compatibility — in the case of GCC until version 5 when the default mode was switched from C90 to C11 (skipping C99). You can still turn off the warning by explicitly requesting C90 mode.