This program goes against everything I have been taught and learned in C. How does this compile? Why does this not need to be int main? Why no return 0? Don't you need an initial declaration of sub() above main? That bugs the crap out of me. I like keeping my functions above main.

#include <stdio.h>

main()
{
   sub ();
   sub ();
}

sub()
{
   static int y = 5;
   printf(" y is %d \n",y);
   y++;
}

Why does this not need to be int main?

Prior to C99, omitting a return type causes the compiler to assume you meant int. This is called "implicit int", and it created enough confusion that C99 removed that "feature".

Why no return 0?

Either the code is correct yet non-portable if the compiler supports falling off main, horribly broken (but not required to report an error) if the compiler doesn't support it, or just fine in C99 and later because this:

int main(void)
{
}

Is equivalent to this:

int main(void)
{
    return 0;
}

Just like in C++.

Don't you need an initial declaration of sub() above main?

Not if the default signature for a function matches your definition, and provided the definition doesn't use variable arguments. I agree with you though, it's not a good practice to exploit this behavior.

How do I check if its c89 or c90?

C89 and C90 are for all practical purposes identical. For your compiler, it depends on your compilation switches which language standard is used. GCC defaults to gnu90, so you can expect C90 with GCC extensions if a switch isn't provided.

This article has been dead for over six months. Start a new discussion instead.