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?

4
  • 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. Commented Dec 5, 2014 at 7:45
  • enable all warnings first before compiling stackoverflow.com/questions/26488502/… stackoverflow.com/questions/8440816/… stackoverflow.com/questions/8220463/… Commented Dec 5, 2014 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. en.wikipedia.org/wiki/C11_%28C_standard_revision%29 I already provided the version I'm using. Commented Dec 6, 2014 at 0:26
  • @coolstuff Yes replace with the most current standard your compiler supports, in your case probably -std=c99. Commented Dec 6, 2014 at 9:09

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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]
 }

1 Comment

Similarly, -std=c99 -pedantic-errors will not compile either.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.