By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 2,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

Function call before main.

P: n/a
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.

Oct 21 '07 #1
Share this Question
Share on Google+
34 Replies


P: n/a
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.hfor 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.

Oct 21 '07 #2

P: n/a
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
Oct 21 '07 #3

P: n/a
santosh <sa*********@gmail.comwrites:
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?
Oct 21 '07 #4

P: n/a
Richard wrote:
santosh <sa*********@gmail.comwrites:
>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
es*****@ieee-dot-org.invalid
Oct 21 '07 #5

P: n/a
Eric Sosman <es*****@ieee-dot-org.invalidwrites:
Richard wrote:
>santosh <sa*********@gmail.comwrites:
>>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?
Oct 21 '07 #6

P: n/a
Richard wrote:
Eric Sosman <es*****@ieee-dot-org.invalidwrites:
>Richard wrote:
>>santosh <sa*********@gmail.comwrites:

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.
Oct 21 '07 #7

P: n/a
Richard wrote:
Eric Sosman <es*****@ieee-dot-org.invalidwrites:
>Richard wrote:
>>santosh <sa*********@gmail.comwrites:

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
es*****@ieee-dot-org.invalid
Oct 21 '07 #8

P: n/a
On Oct 21, 3:57 pm, Srinu <sinu.nayak2...@gmail.comwrote:
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.

Oct 21 '07 #9

P: n/a
abhy <ab************@gmail.comwrites:
On Oct 21, 3:57 pm, Srinu <sinu.nayak2...@gmail.comwrote:
<snip: "can a file-scope variable be initialised with the
result of a function call?">
>>
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?

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.

--
Ben.
Oct 21 '07 #10

P: n/a
In article <87************@bsb.me.uk>,
Ben Bacarisse <be********@bsb.me.ukwrote:
....
>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:
>>printf("%d", x);
???

I thought that was CLC SOP.

Oct 21 '07 #11

P: n/a
Eric Sosman <es*****@ieee-dot-org.invalidwrites:
Richard wrote:
>Eric Sosman <es*****@ieee-dot-org.invalidwrites:
>>Richard wrote:
santosh <sa*********@gmail.comwrites:

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?"
Seems reasonably clear :( Where would you use it in that form where
another form explicitly stating the arguments would be better?
Oct 21 '07 #12

P: n/a
Richard wrote:
Eric Sosman <es*****@ieee-dot-org.invalidwrites:
>Richard wrote:
>>Eric Sosman <es*****@ieee-dot-org.invalidwrites:

Richard wrote:
santosh <sa*********@gmail.comwrites:
>
>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?"

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.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 21 '07 #13

P: n/a
On Oct 21, 3:57 am, Srinu <sinu.nayak2...@gmail.comwrote:
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.

Oct 21 '07 #14

P: n/a
On Sun, 21 Oct 2007 10:57:41 -0000, Srinu <si************@gmail.com>
wrote in comp.lang.c:
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
Oct 22 '07 #15

P: n/a
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;
}

Oct 22 '07 #16

P: n/a
On 21 Oct, 12:27, pete <pfil...@mindspring.comwrote:
Srinu wrote:
Can we assign return value of a function to a global variable?

It's undefined.
is it undefined or a constraint violation. Is the compiler obliged to
produce a diagnostic?

<snip>

--
Nick Keighley

Oct 22 '07 #17

P: n/a
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 22 '07 #18

P: n/a
James Kuyper Jr. said:
Richard Heathfield wrote:
<snip>
>This is not valid C code. The rules of C do not give you licence [...]

He indicated that he was using a C++ compiler.
Er, maybe. This is a classic muddy pool, in that Borland call the compiler
"Turbo C++" nowadays, but it incorporates a C compiler, too. Some people
refer to the compiler as "Turbo C++" even though they are writing in C and
using the C compiler to compile the code.

This happens with Microsoft too. In fact, I myself sometimes refer to using
"Visual C++" when compiling C code using that product. And I /am/ using
(the C compiler that is incorporated in) "Visual C++". It would of course
be possible to refer to "Visual C" instead (and indeed sometimes I do),
but although that would be more accurate in one sense, it would be less
accurate in another, since Microsoft do not ship and (AFAIK) have never
shipped a product called "Visual C".

The real world conspires against precision in communication! :-)

<snip>
Why he was asking a C group about the result of using a C++ compiler is
a harder question.
Not if my guess is right - i.e. that he is using "Turbo C++" to name the
product, rather than to describe the precise nature of the compiler.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 22 '07 #19

P: n/a
James Kuyper Jr. wrote:
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.
He indicated that he was using a C++ compiler. [ ... ]
But in the latest post he said he also compiled it in gcc, which is, or
can be, an ISO C compiler.
Oct 22 '07 #20

P: n/a
pete wrote:
>
Srinu wrote:

Hi all,

Can we assign return value of a function to a global variable?

It's undefined.
"Undefined" or "illegal"?
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();
[...]
In Turbo C++ compiler, it gives x = 9; how is this possible?

The code is undefined.
Is the code even legal? Does C99 allow "global" variables to be
initialized to a non-constant?

Perhaps C++ allows it, and he's compiling in C++ mode? (My compiler
allows such a construct when in C++ mode.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Oct 22 '07 #21

P: n/a
santosh wrote:
>
James Kuyper Jr. wrote:
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.
He indicated that he was using a C++ compiler. [ ... ]

But in the latest post he said he also compiled it in gcc, which is, or
can be, an ISO C compiler.
No, he said compiling it in gcc gave him the (expected) error message
"initializer element is not constant".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Oct 22 '07 #22

P: n/a
Kenneth Brody said:
santosh wrote:
>James Kuyper Jr. wrote:
<snip>
He indicated that he was using a C++ compiler. [ ... ]

But in the latest post he said he also compiled it in gcc, which is, or
can be, an ISO C compiler.

No, he said compiling it in gcc gave him the (expected) error message
"initializer element is not constant".
Er, so is it your position that he did *not* compile it in gcc, *and* that
compiling it in gcc gave him the error message? Because, if so, you just
lost me completely.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 22 '07 #23

P: n/a
Kenneth Brody wrote On 10/22/07 09:41,:
pete wrote:
>>Srinu wrote:
>>>Hi all,

Can we assign return value of a function to a global variable?

It's undefined.


"Undefined" or "illegal"?
"Illegal"[*] because it contravenes a "shall" in a
constraint section and thus requires a diagnostic.

"Undefined" because if the implementation accepts
it anyhow (after issuing the required diagnostic), the
Standard does not define the behavior.
[*]The Standard uses the word "illegal" only once, and
not in reference to a source construct (besides, it's in
a footnote and hence non-normative). But it's fun to imagine
a jurisdiction where the Standard had the force of law ...
"Hands off that keyboard! Step away from the monitor,
keeping your hands in view, that's right. I'm taking you in
on a charge of violating 6.2.2p3, using an identifier with
both internal and external linkage in the same translation
unit. Cuff him, Dennis, and read him his rights. You, my
friend, are going to spend a few years behind ISO-bars."

--
Er*********@sun.com
Oct 22 '07 #24

P: n/a
Richard Heathfield wrote:
Kenneth Brody said:
santosh wrote:
James Kuyper Jr. wrote:
<snip>
He indicated that he was using a C++ compiler. [ ... ]

But in the latest post he said he also compiled it in gcc, which is, or
can be, an ISO C compiler.
No, he said compiling it in gcc gave him the (expected) error message
"initializer element is not constant".

Er, so is it your position that he did *not* compile it in gcc, *and* that
compiling it in gcc gave him the error message? Because, if so, you just
lost me completely.
More precisely, he attempted to compile it in gcc, and the attempt
failed, with an error message.

Oct 22 '07 #25

P: n/a
Eric Sosman wrote:
[...]
[*]The Standard uses the word "illegal" only once, and
not in reference to a source construct (besides, it's in
a footnote and hence non-normative). But it's fun to imagine
a jurisdiction where the Standard had the force of law ...
"Hands off that keyboard! Step away from the monitor,
[...]
unit. Cuff him, Dennis, and read him his rights. You, my
friend, are going to spend a few years behind ISO-bars."
The pressure to come up with a followup pun is too great to bear.
I don't think I can weather the consequences of not posting.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Oct 22 '07 #26

P: n/a
Richard Heathfield wrote:
>
Kenneth Brody said:
santosh wrote:
James Kuyper Jr. wrote:
<snip>
He indicated that he was using a C++ compiler. [ ... ]

But in the latest post he said he also compiled it in gcc, which is, or
can be, an ISO C compiler.
No, he said compiling it in gcc gave him the (expected) error message
"initializer element is not constant".

Er, so is it your position that he did *not* compile it in gcc, *and* that
compiling it in gcc gave him the error message? Because, if so, you just
lost me completely.
Well, quoting the OP:
>>I tried the following...It gives the same value 9 in TCC but showing
error: initializer element is not constant in gcc.
I am, of course, assuming that "in gcc" is not actually part of
the error message, but rather a modifier to the verb "showing".

I suppose the correct statement would be that he ran gcc, in an
attempt to compile it, but the attempt failed with the stated
error message.

Does the term "compiling" require that the compilation succeeded?
Isn't it just as valid to say "I was cooking dinner, but I burned
the chicken"? Even though the cooking failed when it enountered
the error, I don't think it's necessary to qualify the statement
by saying "I attempted to cook dinner" instead.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Oct 22 '07 #27

P: n/a
Kenneth Brody said:
Richard Heathfield wrote:
>>
Kenneth Brody said:
santosh wrote:
James Kuyper Jr. wrote:
<snip>
He indicated that he was using a C++ compiler. [ ... ]

But in the latest post he said he also compiled it in gcc, which is,
or can be, an ISO C compiler.

No, he said compiling it in gcc gave him the (expected) error message
"initializer element is not constant".

Er, so is it your position that he did *not* compile it in gcc, *and*
that compiling it in gcc gave him the error message? Because, if so, you
just lost me completely.

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

I am, of course, assuming that "in gcc" is not actually part of
the error message, but rather a modifier to the verb "showing".

I suppose the correct statement would be that he ran gcc, in an
attempt to compile it, but the attempt failed with the stated
error message.

Does the term "compiling" require that the compilation succeeded?
Isn't it just as valid to say "I was cooking dinner, but I burned
the chicken"? Even though the cooking failed when it enountered
the error, I don't think it's necessary to qualify the statement
by saying "I attempted to cook dinner" instead.
My confusion came from the fact that santosh said that the OP had compiled
it in gcc, and you said "No", before going on to agree that he had
compiled it in gcc. This puzzled me somewhat. Anyway, it seems you do now
agree that the OP *did* compile it in gcc, at least for a certain value of
"compile".

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 22 '07 #28

P: n/a
Richard Heathfield wrote:
>
Kenneth Brody said:
Richard Heathfield wrote:
>
Kenneth Brody said:
santosh wrote:
James Kuyper Jr. wrote:
<snip>

He indicated that he was using a C++ compiler. [ ... ]

But in the latest post he said he also compiled it in gcc, which is,
or can be, an ISO C compiler.

No, he said compiling it in gcc gave him the (expected) error message
"initializer element is not constant".
[...]
My confusion came from the fact that santosh said that the OP had compiled
it in gcc, and you said "No", before going on to agree that he had
compiled it in gcc. This puzzled me somewhat. Anyway, it seems you do now
agree that the OP *did* compile it in gcc, at least for a certain value of
"compile".
Well, I read santosh's post to mean that he thought the OP had
_successfully_ compiled it in gcc, hence my "no". Perhaps I was
mistaken in what santosh meant?

In any case, I think we're all in agreement that initializing a
"global" variable with a function call is not valid C, though it
may be a valid extension in some C compilers. It also appears
that it may be valid C++, but one would have to ask down the hall
for a definitive answer.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Oct 22 '07 #29

P: n/a
Kenneth Brody said:
Richard Heathfield wrote:
<snip>
>My confusion came from the fact that santosh said that the OP had
compiled it in gcc, and you said "No", before going on to agree that he
had compiled it in gcc. This puzzled me somewhat. Anyway, it seems you
do now agree that the OP *did* compile it in gcc, at least for a certain
value of "compile".

Well, I read santosh's post to mean [...]
Oh deary deary me. :-) Let's just get some whitewash, and paint out this
whole subthread, shall we?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 22 '07 #30

P: n/a
Nick Keighley wrote:
pete <pfil...@mindspring.comwrote:
>Srinu wrote:
>>Can we assign return value of a function to a global variable?

It's undefined.

is it undefined or a constraint violation. Is the compiler obliged
to produce a diagnostic?
Of course it is legal. Example follows (untested):

#include <stdio.h>

int value;

int funct(void) {
return 123;
}

int main(void) {

value = 12;
printf("value = %d\n", value);
value = funct();
printf("value = %d\n", value);
return 0;
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 23 '07 #31

P: n/a
Kenneth Brody <ke******@spamcop.netwrote:
Eric Sosman wrote:
[...]
[*]The Standard uses the word "illegal" only once, and
not in reference to a source construct (besides, it's in
a footnote and hence non-normative). But it's fun to imagine
a jurisdiction where the Standard had the force of law ...
"Hands off that keyboard! Step away from the monitor,
[...]
unit. Cuff him, Dennis, and read him his rights. You, my
friend, are going to spend a few years behind ISO-bars."

The pressure to come up with a followup pun is too great to bear.
I don't think I can weather the consequences of not posting.
Oh, do stop raining on Eric's parade. He was only trying to bring a
bright spot to this group.

Richard
Oct 23 '07 #32

P: n/a
Dear All,

I used Turbo C++ compiler but the code was written in a C file and
compiled. So as Richard said, the compiler might have used the inbuilt
C compiler. What I think is, may be the saparation between the inbuilt
C compiler and C++ compiler in Turbo package, not be totally different
and because of that it gives some machine code which will let the user
know that this kind of code is compiler dependent and not standard
defined. But gcc denies to generate the machine code even! It's OK.

To answer friend Richard's question "what it is that you are trying to
do" :

Basically I came across a code in C++ which creates a global variable
as x= new My_Object(); i thought if it has to work, the constructor
must be called before the main(). But how is that possible? I
immediately took my tcc compiler and tried to execute the above code.
To my surprise it gave 9. So I thought of geeting experts suggestion
on whether C standard says anything about it or not. Later I came to
know that C++ allows it. I too couldn't understand, in C++, it is only
the constructors are allowed or any function. But I couldn't dare to
ask that in this C group.

To answer friend James question "Why he was asking a C group about the
result of using a C++ compiler is a harder question." :

The situation is little odd here. I asked this question in C group
only, thinking that anyway the code is in C and I was looking for the
answer on how a C code should behave. but it is wrong as my friend
points, why to discuss about a C++ compiler. Ok, if I would have asked
the question in C++ group, there obviously the question might have
arised "why there is a question about C code in C++ group?". So
finally where to post? I agree it is my fault, I wrote a C code and
compiled in a C++ compiler. But where to go now? As friend Richard
says, now a days mostly/easily available C/C++ compilers incorporate
these two compilers both. even gcc can compile two languages code,
even more than that. So with confusion I had to post it here.I would
like to apologize if I am wrong.
Thanks a lot to all of friends.
With regards.
Srinu

Oct 23 '07 #33

P: n/a
Srinu said:

<snip>
To answer friend Richard's question "what it is that you are trying to
do" :

Basically I came across a code in C++ which creates a global variable
as x= new My_Object(); i thought if it has to work, the constructor
must be called before the main(). But how is that possible?
It *has* to be possible in C++, for reasons which you're better off
discovering in comp.lang.c++ if your C++ book doesn't tell you about them.
And because it *has* to be possible, they made it possible.

It doesn't need to be possible in C, and so they didn't make it possible in
C. If your C compiler lets you do it, this is an extension provided by
that compiler, not a legal C construct.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 23 '07 #34

P: n/a
I think it's more likely that you unknowingly invoked the compiler in
C++ mode. Many C/C++ compilers automatically default to C++ for files
with extensions like .cpp or .c++, unless you explicitly tell them
otherwise. What name did you use for your file?
I had used .c for my file name.

Srinu.

Oct 24 '07 #35

This discussion thread is closed

Replies have been disabled for this discussion.