8 Replies - 974 Views - Last Post: 05 December 2014 - 12:45 PM Rate Topic: -----

#1 COKEDUDE   User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 274
  • Joined: 01-February 10

Strange program compiles

Posted 04 December 2014 - 08:52 PM

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



Is This A Good Question/Topic? 0
  • +

Replies To: Strange program compiles

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15927
  • View blog
  • Posts: 63,731
  • Joined: 12-June 08

Re: Strange program compiles

Posted 04 December 2014 - 08:55 PM

Context - where did you find this?
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: Strange program compiles

Posted 04 December 2014 - 10:11 PM

Leaving out the return type of a function used to be valid in old versions of C. The return type would then simply be int by default. This is no longer allowed by modern standards.

Similarly it used to be legal to call a function that has not been declared. Again it would then be assumed that the return type of that function was int (and the arity and argument types were whatever arity and argument types you used to call it). If you later defined the function with a different return type, arity or argument types, that'd invoke undefined behavior. Again modern standards no longer allow this.

Leaving out the return statement in a function whose return value is being used (that includes main) invokes undefined behavior, but generally doesn't prevent your code from compiling.
Was This Post Helpful? 1
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5882
  • View blog
  • Posts: 17,873
  • Joined: 25-December 09

Re: Strange program compiles

Posted 04 December 2014 - 10:33 PM

What might surprise you even more is that the following will compile with a C90 compiler:

#include <stdio.h>

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

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




I wouldn't trust the parameter values but it should compile and run.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 COKEDUDE   User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 274
  • Joined: 01-February 10

Re: Strange program compiles

Posted 04 December 2014 - 11:50 PM

View Postmodi123_1, on 04 December 2014 - 08:55 PM, said:

Context - where did you find this?


It was a static example.

View Postsepp2k, on 04 December 2014 - 10:11 PM, said:

Leaving out the return type of a function used to be valid in old versions of C. The return type would then simply be int by default. This is no longer allowed by modern standards.

Similarly it used to be legal to call a function that has not been declared. Again it would then be assumed that the return type of that function was int (and the arity and argument types were whatever arity and argument types you used to call it). If you later defined the function with a different return type, arity or argument types, that'd invoke undefined behavior. Again modern standards no longer allow this.

Leaving out the return statement in a function whose return value is being used (that includes main) invokes undefined behavior, but generally doesn't prevent your code from compiling.


How did it compile then? This is the version info.

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


It looks quite old.

https://www.gnu.org/...c/releases.html

How do I check if its c89 or c90?

View Postjimblumberg, on 04 December 2014 - 10:33 PM, said:

What might surprise you even more is that the following will compile with a C90 compiler:

#include <stdio.h>

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

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




I wouldn't trust the parameter values but it should compile and run.

Jim


Any idea why this bad code compiles?
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5882
  • View blog
  • Posts: 17,873
  • Joined: 25-December 09

Re: Strange program compiles

Posted 05 December 2014 - 07:58 AM

Quote

Any idea why this bad code compiles?

It is not "bad" code. It is "perfectly" acceptable C90 code. This version of the C standard accepts this code because if you don't specify the types of the parameters it assumes that they are ints. And remember in C if you specify a function with no parameters this signifies that the function with any number of parameters. So what the compiler "sees" is this:

int main()
{
   sub ();
   sub (2);

   return(0);
}

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



Now although the original code is "acceptable" if I actually tried to use the return value from sub() I would be invoking undefined behavior. And if the parameter is not passed to the function using them also invokes undefined behavior. And my modern compiler does issue warnings about these issues.

||=== c_homework, Debug ===|
main.c|3|warning: return type defaults to ‘int’ [-Wreturn-type]|
main.c||In function ‘main’:|
main.c|5|warning: implicit declaration of function ‘sub’ [-Wimplicit-function-declaration]|
main.c|11|warning: return type defaults to ‘int’ [-Wreturn-type]|
main.c|11|warning: no previous declaration for ‘sub’ [-Wmissing-declarations]|
main.c||In function ‘sub’:|
main.c|11|warning: type of ‘a’ defaults to ‘int’ [-Wmissing-parameter-type]|
main.c|11|warning: type of ‘b’ defaults to ‘int’ [-Wmissing-parameter-type]|
main.c||In function ‘main’:|
main.c|9|warning: control reaches end of non-void function [-Wreturn-type]|
main.c||In function ‘sub’:|
main.c|20|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 0 errors, 8 warnings (0 minutes, 0 seconds) ===|



Jim
Was This Post Helpful? 0
  • +
  • -

#7 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1652
  • View blog
  • Posts: 8,524
  • Joined: 07-September 06

Re: Strange program compiles

Posted 05 December 2014 - 11:00 AM

I believe it is also important to note most, if not all, C compilers are backwards compatible. So, as long as you use a valid standard of C even the latest compiler will successfully compile the code.

Now, that doesn't mean that all C code is also valid C++ code. If you attempt to use g++ to compile the example jim gave, it will error out:

Quote

main.c: In function ‘int main()’:
main.c:5:10: error: ‘sub’ was not declared in this scope
sub ();
^
main.c: At global scope:
main.c:9:4: error: expected constructor, destructor, or type conversion before ‘(’ token
sub(a)

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5882
  • View blog
  • Posts: 17,873
  • Joined: 25-December 09

Re: Strange program compiles

Posted 05 December 2014 - 11:20 AM

Quote

I believe it is also important to note most, if not all, C compilers are backwards compatible.

No, this is not true. A current C compiler using the current C standard should fail to compile any of the code posted in this topic. The current C standard no longer accepts default arguments, and there are some other differences as well. For example a C11 compiler should no longer recognize the gets() function since it has actually been removed from the standard. But most compilers can be set to use the earlier standards for compatibility reasons, and unfortunately many of these modern compilers default to using the C90 standard. And some compilers actually don't support any of the latest standards. The Microsoft C compiler only offers complete C90 support, the gcc compiler uses a "switch" to force compiling with the latest standards -std=c99 or -std=c11.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1652
  • View blog
  • Posts: 8,524
  • Joined: 07-September 06

Re: Strange program compiles

Posted 05 December 2014 - 12:45 PM

Quote

...unfortunately many of these modern compilers default to using the C90 standard...

Ah, that's the part I didn't know. Seems dumb that they'd continue defaulting to an extremely old standard though. I suppose that also explains why gcc compiled it nicely right out of the gate (I didn't try forcing a standard switch, I assumed that it was using 99 or higher by default).

Good to know.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1