2

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++;
}

The gcc version is:

gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)

This is an old version it seems but not to crazy old.

https://www.gnu.org/software/gcc/releases.html

How do I check if this is c90 or c89?

cokedude
  • 379
  • 1
  • 11
  • 21
  • 1
    All these gcc questions about "how can this be allowed by the C standard" are getting tiresome... **If you do not compile with `gcc -std=c11 -pedantic-errors` then gcc is not a strictly conforming C compiler**, but a non-standard one compiling against a ton of weird GNU extensions. – Lundin Dec 05 '14 at 07:45
  • enable all warnings first before compiling http://stackoverflow.com/questions/26488502/which-section-in-c89-standard-allows-the-implicit-int-rule http://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function http://stackoverflow.com/questions/8220463/c-function-calls-understanding-the-implicit-int-rule – phuclv Dec 05 '14 at 10:05
  • @Lundin Should I be using this instead then `gcc -std=c90 -pedantic-errors`? Wikipedia is making it sound like c11 isn't supported till gcc version 4.6. http://en.wikipedia.org/wiki/C11_%28C_standard_revision%29 I already provided the version I'm using. – cokedude Dec 06 '14 at 00:26
  • @coolstuff Yes replace with the most current standard your compiler supports, in your case probably -std=c99. – Lundin Dec 06 '14 at 09:09

3 Answers3

4

This code uses an obsolete feature of early C called implicit int. Its only uses are in code-golf competitions. Indeed, even variables may be declared this way. The variable y might easily have been declared

static y = 5;

.

A function may be called without having been prototyped. The function is assumed to receive exactly the number of arguments passed, subject to the "usual promotions". Any type smaller than an int is promoted to int, and floats are promoted to double.

So the functions behave as if they were prototyped as:

int main(void);
int sub(void);

To return any type other than int, the return type must be specified.


You can specify the standard you wish to use when compiling.

gcc -ansi
gcc -std=c99

And add -pedantic to make gcc believe that you really mean it.


Oddly enough, this code does not conform strictly to any standard. C99 disallows implicit int, but permits eliding return 0; from main. C90 or "ansi" C allows implicit int, but requires a return. So, a return should definitely be there.

Btw, C89 is exactly the same thing as C90. It took a while for both hemispheres of the world to agree. Timezones and meridians and such. It's the same standard.

luser droog
  • 18,988
  • 3
  • 53
  • 105
1

It does not compile. If you don't tell gcc to be a strictly conforming C compiler but just call it with gcc test.c, then it will remain a completely non-standard compiler which allows a whole lot of weird things. It does not conform to any known C standard when left with its default setting.

gcc -std=c11 -pedantic-errors gives:

test.c:3:1: error: return type defaults to 'int'
 main()
 ^
test.c: In function 'main':
test.c:5:4: error: implicit declaration of function 'sub' [-Wimplicit-function-d
eclaration]
    sub ();
    ^
test.c: At top level:
test.c:9:1: error: return type defaults to 'int'
 sub()
 ^
test.c: In function 'sub':
test.c:14:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

@lundin, please note:

main()

is not equivalent to

int main(void)

because void means there are no parameters but () means there can be any number of parameters.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41