Function call before main.

S

Srinu

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

santosh

Srinu said:
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);

}

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 said:
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.
 
P

pete

Srinu said:
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.
 
M

Malcolm McLean

Srinu said:
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.
 
E

Eric Sosman

Richard said:
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.
 
R

Richard

Eric Sosman said:
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?
 
J

James Kuyper Jr.

Richard said:
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.


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

Eric Sosman

Richard said:
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.
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?"
 
A

abhy

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

Ben Bacarisse

abhy said:
On Oct 21, 3:57 pm, Srinu <[email protected]> wrote:
<snip: "can a file-scope variable be initialised with the
result of a function call?">
In f() x is a local variable and thus takes priority over global
variable x.

No it isn't -- x has "file scope".

The question has been well answered. The only thing that I can might
be worth adding is that neither int valued function contains a return
statement.
 
K

Kenny McCormack

Ben Bacarisse said:
The question has been well answered. The only thing that I can might
be worth adding is that neither int valued function contains a return
statement.

You mean you're not going to bitch about the lack of a newline in:

???

I thought that was CLC SOP.
 
R

Richard

Eric Sosman said:
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.


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

Seems reasonably clear :( Where would you use it in that form where
another form explicitly stating the arguments would be better?
 
E

Eric Sosman

Richard said:
Seems reasonably clear :( Where would you use it in that form where
another form explicitly stating the arguments would be better?

You would use such a declaration in connection with
pre-Standard code, because "another form" was not always
available. In particular, if you need to write a declaration
for an old-style function like

int f(x)
enum States x;
{ ... }

then `int f()' is pretty much your only choice.

You might also use it when declaring function pointers
that might point to functions of different types:

int f(int);
int g(double);
int h(const char*);

int (*fp)() = (int(*)()) g;

switch (whatIsIt) {
case INT: x = fp(42); break;
case DBL: x = fp(42.0); break;
case STR: x = fp("XLII"); break;
}

Even here, though, it might be better (or even necessary)
to cast the function pointer to get the benefit of prototypes:

switch (whatIsIt) {
case INT: x = ((int(*)(int)) fp)(42); break;
case DBL: x = ((int(*)(double)) fp)(42.0); break;
case STR: x = ((int(*)(const char*)) fp)("XLII"); break;
}

As is often the case with function pointers, a few typedefs
can remove a lot of visual clutter.
 
B

Barry Schwarz

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.

A "global" object has static duration. The initialization of a static
object must be a constant expression or a string literal. The return
value of a function does not qualify. Therefore, the answer to your
question is no and the subsequent discussion is based on a false
premise.

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

Did you invoke the compiler is strictly conforming mode? Many
compilers default to allowing non-standard extensions.
 
J

Jack Klein

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?

Because you used the Turbo C++ compiler to compile this code as C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
S

Srinu

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();

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

int f()
{
return 9;
}
 
R

Richard Heathfield

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.

In other words, you're doing it wrong. Instead of trying to understand why
you get the output you have shown, decide what you are trying to achieve,
and try to establish a legal way to achieve it. The way to do that
depends, of course, and what it is that you are trying to do.
 
J

James Kuyper Jr.

Richard said:
Srinu said:


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top