Re: Function call before main.



Richard Heathfield wrote:
Srinu said:

Hi all,

Thanks a lot for all the answers.

I tried the following...It gives the same value 9 in TCC but showing
error: initializer element is not constant in gcc.

#include<stdio.h>
int f();
int x = f();

This is not valid C code. The rules of C do not give you licence to call a function from any part of the code other than inside another function. If you try to do so anyway, the compiler is free to reject your code, or to translate it in some arbitrary manner which may or may not be predictable on a given implementation.

He indicated that he was using a C++ compiler. The rules of C++ do allow it, and the results can be predictable, except when they depend upon the order of initialization of static objects, which isn't a problem in this example. Some pairs of objects do have guaranteed ordering, others do not - you have to know the relevant rules if you decide to write that kind of code.

Why he was asking a C group about the result of using a C++ compiler is a harder question.
.



Relevant Pages

  • Re: Function call before main.
    ... initializer element is not constant in gcc. ... int x = f; ... can be, an ISO C compiler. ...
    (comp.lang.c)
  • Re: Function call before main.
    ... initializer element is not constant in gcc. ... int x = f; ... can be, an ISO C compiler. ...
    (comp.lang.c)
  • Re: It Pays to Enrich Your C Skills
    ... Check if you can score a perfect 10 (without using a compiler). ... int main{ ... struct bitfield { ... out if it is a negative integer constant or a constant expression ...
    (comp.lang.c.moderated)
  • OT: Re: Perl Peeves
    ... I see the result of a test being used as an int. ... the compiler just assumed you knew what you were doing ... introduced to the language later, so void * was unheard of in most code. ... This didn't mean bool was special, declaring it just signaled to the ...
    (comp.lang.perl.misc)
  • Re: OT: Re: Perl Peeves
    ... when I see the result of a test being used as an int. ... compiler just assumed you knew what you were doing and would ... This didn't mean bool was special, declaring it just signaled to the ... What "normalization of bool results is built into the compiler"? ...
    (comp.lang.perl.misc)