Re: Function call before main.
Srinu wrote:
> Hi all,
>
> Can we assign return value of a function to a global variable? As we
> know, main() will be the first function to be executed. but if the
> above is true, then we have a function call before main. Please
> me calarifying this. The code may be of the form.
>
> int f();
To state explicitly that the function takes no parameters use the `void`
keyword.
int f(void);
> int x = f();
>
> int main()
> {
> printf("%d", x);
Include <stdio.h> for the prototype for printf. Without it, you are
invoking undefined behaviour.
> }
Since main is declared as returning an int, return a value. Use 0 or
EXIT_SUCCESS for sucessful termination and EXIT_FAILURE for abnormal
termination. The macros are defined in <stdlib.h>
> int f()
> {
> x=9;
f() is declared as returning an int and you return nothing here. This is
disallowed under the latest C Standard and can lead to unpredictable
behaviour if you attempt to use the return value of f(), as you've done
so.
If you don't want a function to return a value specify:
void f() ...
> }
>
> In Turbo C++ compiler, it gives x = 9; how is this possible?
By sheer luck.
|