Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Function call before main. (http://www.velocityreviews.com/forums/t545829-function-call-before-main.html)

Srinu 10-21-2007 10:57 AM

Function call before main.
 
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 help
me calarifying this. The code may be of the form.

int f();
int x = f();

int main()
{
printf("%d", x);
}

int f()
{
x=9;
}

In Turbo C++ compiler, it gives x = 9; how is this possible?

Srinu.


santosh 10-21-2007 11:10 AM

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 help
> 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.


pete 10-21-2007 11:27 AM

Re: Function call before main.
 
Srinu wrote:
>
> Hi all,
>
> Can we assign return value of a function to a global variable?


It's undefined.

> 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 help
> me calarifying this. The code may be of the form.
>
> int f();
> int x = f();
>
> int main()
> {
> printf("%d", x);
> }
>
> int f()
> {
> x=9;
> }
>
> In Turbo C++ compiler, it gives x = 9; how is this possible?


The code is undefined.

--
pete

Malcolm McLean 10-21-2007 12:02 PM

Re: Function call before main.
 

"Srinu" <sinu.nayak2001@gmail.com> wrote in message
> 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 help
> me calarifying this. The code may be of the form.
>
> int f();
> int x = f();
>
> int main()
> {
> printf("%d", x);
> }
>
> int f()
> {
> x=9;
> }
>
> In Turbo C++ compiler, it gives x = 9; how is this possible?
>

C++ allows you to define global objects, and will call their constructors
before calling main(). The C++ standard was a little bit weak in this
respect last time I checked, with the order in which objects are constructed
not properly defined.

C doesn't allow any functions to be called excpet from main(), but is it
quite possible that your compiler will allow it as an extension. A lot of C
compilers are developed alongside C++ compilers after all.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


Richard 10-21-2007 01:49 PM

Re: Function call before main.
 
santosh <santosh.k83@gmail.com> writes:

> 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 help
>> 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.


What does "f()" state?

Eric Sosman 10-21-2007 02:12 PM

Re: Function call before main.
 
Richard wrote:
> santosh <santosh.k83@gmail.com> writes:
>
>> 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 help
>>> 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.

>
> What does "f()" state?


Do you truly not know? I thought you'd been on this
newsgroup long enough to have seen this mentioned half a
dozen times, but perhaps that's a different "Richard."

It states that the function f takes some fixed number
of arguments, but does not state what that number is nor
what the types of the arguments are.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Richard 10-21-2007 03:26 PM

Re: Function call before main.
 
Eric Sosman <esosman@ieee-dot-org.invalid> writes:

> Richard wrote:
>> santosh <santosh.k83@gmail.com> writes:
>>
>>> 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 help
>>>> 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.

>>
>> What does "f()" state?

>
> Do you truly not know? I thought you'd been on this
> newsgroup long enough to have seen this mentioned half a
> dozen times, but perhaps that's a different "Richard."


Nope. Probably me. And I never knew that. I had always assumed it to be
a lazy definition of f(void), but since I have never used it (I cant
remember the last time I wrote a function without at least one
parameter) then I wasn't sure.

>
> It states that the function f takes some fixed number


Or doesn't state :-;

> of arguments, but does not state what that number is nor
> what the types of the arguments are.


Which is used where?

James Kuyper Jr. 10-21-2007 03:45 PM

Re: Function call before main.
 
Richard wrote:
> Eric Sosman <esosman@ieee-dot-org.invalid> writes:
>
>> Richard wrote:
>>> santosh <santosh.k83@gmail.com> writes:
>>>
>>>> Srinu wrote:

....
>>>>> int f();
>>>> To state explicitly that the function takes no parameters use the `void`
>>>> keyword.
>>> What does "f()" state?

>> Do you truly not know? I thought you'd been on this
>> newsgroup long enough to have seen this mentioned half a
>> dozen times, but perhaps that's a different "Richard."

>
> Nope. Probably me. And I never knew that. I had always assumed it to be
> a lazy definition of f(void), but since I have never used it (I cant
> remember the last time I wrote a function without at least one
> parameter) then I wasn't sure.
>
>> It states that the function f takes some fixed number

>
> Or doesn't state :-;


Well, it is undefined behavior to call f() with a different number of
arguments than the number specified in the definition of f(), or if the
definition of f() makes it a variadic function. So this declaration does
indeed state that the number is fixed.

>> of arguments, but does not state what that number is nor
>> what the types of the arguments are.

>
> Which is used where?


It's only supported to allow compilation of code written before the
invention of proper function prototypes. It serves no good purpose that
is not better served by a function prototype. It can be used to
obfuscate code, if that's your desire.

Eric Sosman 10-21-2007 03:56 PM

Re: Function call before main.
 
Richard wrote:
> Eric Sosman <esosman@ieee-dot-org.invalid> writes:
>
>> Richard wrote:
>>> santosh <santosh.k83@gmail.com> writes:
>>>
>>>> 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 help
>>>>> 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.
>>> What does "f()" state?

>>
>> It states that the function f takes some fixed number

>
> Or doesn't state :-;


No, "states." If declared this way, the function f must
not be variadic (if it is, and if you call it, the behavior
is undefined). So the declaration does in fact state something
about f's argument list. Not much, but more than nothing.

>> of arguments, but does not state what that number is nor
>> what the types of the arguments are.

>
> Which is used where?


I do not understand this question. For starters, what is
the referent of "which?" And what do you mean by "used?"

--
Eric Sosman
esosman@ieee-dot-org.invalid

abhy 10-21-2007 03:58 PM

Re: Function call before main.
 
On Oct 21, 3:57 pm, Srinu <sinu.nayak2...@gmail.com> 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 help
> me calarifying this. The code may be of the form.
>
> int f();
> int x = f();
>
> int main()
> {
> printf("%d", x);
>
> }
>
> int f()
> {
> x=9;
>
> }
>
> In Turbo C++ compiler, it gives x = 9; how is this possible?
>
> Srinu.


In f() x is a local variable and thus takes priority over global
variable x.
Thus when x is assigned a value , it continues to remain as x is also
a global.
If x was declared within main , then 9 will never get printed.



All times are GMT. The time now is 04:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.