Function call before main.

R

Richard Heathfield

James Kuyper Jr. said:
Richard Heathfield wrote:
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! :)

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

Kenneth Brody

pete said:
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:[email protected]>
 
K

Kenneth Brody

santosh said:
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:[email protected]>
 
R

Richard Heathfield

Kenneth Brody said:
santosh said:
James Kuyper Jr. wrote:
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.
 
E

Eric Sosman

Kenneth Brody wrote On 10/22/07 09:41,:
"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."
 
J

jameskuyper

Richard said:
Kenneth Brody said:
santosh said:
James Kuyper Jr. wrote:
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.
 
K

Kenneth Brody

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:[email protected]>
 
K

Kenneth Brody

Richard said:
Kenneth Brody said:
santosh said:
James Kuyper Jr. wrote:
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 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:[email protected]>
 
R

Richard Heathfield

Kenneth Brody said:
Richard said:
Kenneth Brody said:
santosh wrote:
James Kuyper Jr. wrote:
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 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".
 
K

Kenneth Brody

Richard said:
Kenneth Brody said:
Richard said:
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:[email protected]>
 
R

Richard Heathfield

Kenneth Brody said:
Richard Heathfield wrote:
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?
 
C

CBFalconer

Nick said:
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;
}
 
R

Richard Bos

Kenneth Brody said:
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
 
S

Srinu

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
 
R

Richard Heathfield

Srinu said:

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>
 
J

James Kuyper Jr.

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

When a compiler handles both C and C++, there's an incentive for the
implementors to minimize the differences between those two languages.
When code has implementation-defined or undefined behavior in at least
one of the two languages, then it sometimes simplifies things for the
implementors if they choose the same behavior in both languages.
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.

The initializer for a static object in C++ can be just about any
expression that can be implicitly converted to the declared type of the
object. My copy of the C++ standard is stuck on the hard drive of a
non-functional computer, thanks to a failed Windows update, so I can't
quote you the precise rules. However, the main issue you would have to
worry about is order of initialization. Static objects are initialized
in an order that is only partially constrained by the C++ standard; if
evaluation of the initializer expression for one object requires
retrieval of the value of another static object that hasn't been
initialized yet, the behavior is undefined.
To answer friend James question "Why he was asking a C group about the
result of using a C++ compiler is a harder question." :

Well, I considered that your compiler might have allowed you, as an
extension, to do the same thing in C that you can in C++. However, since
it's a constraint violation in C (6.7.8p4), I didn't think that was
likely. Most compilers treat constraint violations as fatal errors,
though all that the standard requires is a diagnostic message, which
need not be fatal.

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

You could have cross-posted this question to one of the C++ newsgroups,
if you were aware that this was a C++ specific behavior. However, if you
had known that it was C++-specific, you would not have had to ask your
question in the first place. So don't worry about it.
 
S

Srinu

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.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top