help w/ c/c++ problem

  • Thread starter Heinrich Pumpernickel
  • Start date
C

CBFalconer

J. J. Farrell said:
Not really; isn't that simply down to the number of C textbooks
which explicitly say that defining main as void is fine? I don't
think many of them mention the environment extension.

If a so-called "C textbook" says "void main()" is valid and
portable, it isn't a C textbook. It may be a nearly-C textbook.
Unload it for something like K&R II.
 
M

Mark McIntyre

I've read the entire thread. Google Groups says you've posted nine
responses in this thread; I don't know which one you're referring to.

Possibly Message-ID: <[email protected]>
though as I don't retrieve my own posts and note the message numbers.,
it may not be.

In any events the post refers to some revised wording of 5.1.2.2.3p1
dug out by Harald van D?k which makes it clear that main is allowed to
return non-int types.
or the return type of the /main/ function is
not a type compatible with /int/, the termination status returned =20
to the host environment is unspecified.

My response was:
Excellent - thats interesting and thanks for pointing it out to me. I
quite agree it clarifies that a hosted env to provide an
implementation defined non-int return type. This is evidently a change
from C89, and as such is a welcome clarification.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
K

Keith Thompson

Mark McIntyre said:
Possibly Message-ID: <[email protected]>
though as I don't retrieve my own posts and note the message numbers.,
it may not be.

In any events the post refers to some revised wording of 5.1.2.2.3p1
dug out by Harald van D?k which makes it clear that main is allowed to
return non-int types.


My response was:

Followed by:
| So I agree with you, a hosted implementation can define a non-int
| return type for main. However I stress, it is implementation defined,
| non standardised and nonportable. As such I stand by my original
| comment.

I agree with the first two sentences.
 
P

pete

Army1987 said:
Army1987 said:
On Fri, 06 Jul 2007 10:49:20 -0700, Keith Thompson wrote: [...]
Well, pre-ANSI C had no 'void' keyword. For a function not intended to
return a result, it was common to declare it with no explicit return

But I think in C89 a main() with no return caused UB...

Not quite; it merely returns an undefined termination status to the
host environment.
Which will try to use it to call exit(), causing UB.

As far as the C programming language is concerned,
no behavior is defined subsequent to the return of main.
 
A

Army1987

As far as the C programming language is concerned,
no behavior is defined subsequent to the return of main.
army1987@army1987-laptop:~$ cat foo.c
#include <stdlib.h>
#include <stdio.h>
static char *global;
void frob(void)
{
puts("Hi, I'm frob().");
puts("If main() hasn't returned yet, global points to local, "
"which is still intact.");
puts("Now, the string pointed to by global is:");
puts(global);
}

int main(void)
{
char local[] = "Hello, world!";
global = local;
atexit(frob);
return 0;
}
[1]+ Done gedit foo.c
army1987@army1987-laptop:~$ gcc foo.c -ansi -pedantic -Wall -Wextra -o foo
army1987@army1987-laptop:~$ ./foo
Hi, I'm frob().
If main() hasn't returned yet, global points to local, which is still intact.
Now, the string pointed to by global is:
��llok���r��
army1987@army1987-laptop:~$

Looks like local has already died when frob() is called. Yes, the
behaviour is undefined, but if I comment out the last line of the
body of frob() it correctly displays all the stuff before it.
 
B

Ben Bacarisse

Army1987 said:
Army1987 said:
On Fri, 06 Jul 2007 10:49:20 -0700, Keith Thompson wrote: [...]
Well, pre-ANSI C had no 'void' keyword. For a function not intended to
return a result, it was common to declare it with no explicit return

But I think in C89 a main() with no return caused UB...

Not quite; it merely returns an undefined termination status to the
host environment.
Which will try to use it to call exit(), causing UB.

I don't think so. My draft (n869) says:

If the return type of the main function is a type compatible with
int, a return from the initial call to the main function is
equivalent to calling the exit function with the value returned by
the main function as its argument; reaching the } that terminates
the main function returns a value of 0. If the return type is not
compatible with int, the termination status returned to the host
environment is unspecified.

so, as I read it,

int main(void) { return X; } equivalent to exit(X);
int main(void) { } "returns a value of 0"
void main(void) { } unspecified termination status
 
C

CBFalconer

Army1987 said:
As far as the C programming language is concerned,
no behavior is defined subsequent to the return of main.

#include <stdlib.h>
#include <stdio.h>
static char *global;
void frob(void) {
puts("Hi, I'm frob().");
puts("If main() hasn't returned yet, global points to local, "
"which is still intact.");
puts("Now, the string pointed to by global is:");
puts(global);
}

int main(void) {
char local[] = "Hello, world!";
global = local;
atexit(frob);
return 0;
}
[1]+ Done gedit foo.c
army1987@army1987-laptop:~$ gcc foo.c -ansi -pedantic -Wall -Wextra -o foo
army1987@army1987-laptop:~$ ./foo
Hi, I'm frob().
If main() hasn't returned yet, global points to local, which is still intact.
Now, the string pointed to by global is:
��llok���r��
army1987@army1987-laptop:~$

Looks like local has already died when frob() is called. Yes, the
behaviour is undefined, but if I comment out the last line of the
body of frob() it correctly displays all the stuff before it.

Try a correct instruction to copy global to local. This needs
strcpy, and a suitable #include.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

CBFalconer said:
Army1987 said:
As far as the C programming language is concerned,
no behavior is defined subsequent to the return of main.

#include <stdlib.h>
#include <stdio.h>
static char *global;
void frob(void) {
puts("Hi, I'm frob().");
puts("If main() hasn't returned yet, global points to local, "
"which is still intact.");
puts("Now, the string pointed to by global is:");
puts(global);
}

int main(void) {
char local[] = "Hello, world!";
global = local;
atexit(frob);
return 0;
}
[1]+ Done gedit foo.c
army1987@army1987-laptop:~$ gcc foo.c -ansi -pedantic -Wall -Wextra -o
foo army1987@army1987-laptop:~$ ./foo
Hi, I'm frob().
If main() hasn't returned yet, global points to local, which is still
intact. Now, the string pointed to by global is:
��llok���r��
army1987@army1987-laptop:~$

Looks like local has already died when frob() is called. Yes, the
behaviour is undefined, but if I comment out the last line of the
body of frob() it correctly displays all the stuff before it.

Try a correct instruction to copy global to local. This needs
strcpy, and a suitable #include.

When fixed, the program would be irrelevant to the point Army1987 was
making. It was intentionally invalid code.
 
K

Keith Thompson

Ben Bacarisse said:
I don't think so. My draft (n869) says:

If the return type of the main function is a type compatible with
int, a return from the initial call to the main function is
equivalent to calling the exit function with the value returned by
the main function as its argument; reaching the } that terminates
the main function returns a value of 0. If the return type is not
compatible with int, the termination status returned to the host
environment is unspecified.

so, as I read it,

int main(void) { return X; } equivalent to exit(X);
int main(void) { } "returns a value of 0"
void main(void) { } unspecified termination status

The latter generates an unspecified termination status *if* the
implementation supports and documents 'void main(void)'. If it
doesn't, the behavior is undefined. (The consequence could include
generating an unspecified termination status, of course.)
 
B

Ben Bacarisse

Keith Thompson said:
The latter generates an unspecified termination status *if* the
implementation supports and documents 'void main(void)'. If it
doesn't, the behavior is undefined. (The consequence could include
generating an unspecified termination status, of course.)

Yes. I did not want to re-open that can of worms. An undocumented
form of main is UB (and non-portable). I don't think I implied
otherwise -- I certainly didn't mean to.

A lot of context have been lost. I was giving chapter and verse in
support of your statement in this exchange:

Army1987:
||| But I think in C89 a main() with no return caused UB...
You:
|| Not quite; it merely returns an undefined termination status to the
|| host environment.
Army1987:
| Which will try to use it to call exit(), causing UB.

I just wanted to show that C89 does not define the meaning of a main
with no return in terms of a call to exit().
 
P

pete

army1987@army1987-laptop:~$ cat foo.c
#include <stdlib.h>
#include <stdio.h>
static char *global;
void frob(void)
{
puts("Hi, I'm frob().");
puts("If main() hasn't returned yet, global points to local, "
"which is still intact.");
puts("Now, the string pointed to by global is:");
puts(global);
}

int main(void)
{
char local[] = "Hello, world!";
global = local;
atexit(frob);

puts("I don't understand what your point is.");
puts("This program terminates before main can return.");
 
P

pete

pete said:
army1987@army1987-laptop:~$ cat foo.c
#include <stdlib.h>
#include <stdio.h>
static char *global;
void frob(void)
{
puts("Hi, I'm frob().");
puts("If main() hasn't returned yet, global points to local, "
"which is still intact.");
puts("Now, the string pointed to by global is:");
puts(global);
}

int main(void)
{
char local[] = "Hello, world!";
global = local;
atexit(frob);

puts("I don't understand what your point is.");
puts("This program terminates before main can return.");

I'm having a bad week, making a lot of mistakes.
I actually am starting to see your point.
 
U

U

[ discussing validness of `void main(void) { /* ... */ }' ]
its not just some book, its called "C: The Complete Reference",
written by some famous and internationally renowed autor
("...world's leading programming author...", "...is a leading
authority on C and was a member of the ANSI/ISO committee that
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
standardized C...") and it spicifically says.......... ^^^^^^^^^^^^^^

You may also declare main() as void if it does not
return a value.

Question to the NG: This is the first time ever I've heard
H.S. was an actual member of a C standardisation group. I've
seen a list of people involved in ANSI C89, but it seems more
difficult to get by a similar list for ISO 9899, both C90 and
C99; can anybody with more insight confirm/debunk this?

[The only source for above quote I could find is the Amazon
review section of `C: The Complete Reference', quote:

"""
*About the Author*
Herbert Schildt is a leading authority on C and was a
member of the ANSI/ISO committee that standardized C. He
is the author of Teach Yourself C, C++: The complete
Reference, Windows 2000 Programming from the Ground Up,
Java: The Complete Reference, and many other best-sellers.
"""
(Source: <http://www.amazon.co.uk/gp/product/...e=UTF8&n=266239&s=books&qid=1183507472&sr=1-8> )]
 
K

Keith Thompson

Ben Bacarisse said:
Yes. I did not want to re-open that can of worms. An undocumented
form of main is UB (and non-portable). I don't think I implied
otherwise -- I certainly didn't mean to.
[...]

Sure. Given recent discussions here, I just thought it was best to be
crystal clear.
 
B

Ben Pfaff

U said:
Question to the NG: This is the first time ever I've heard
H.S. was an actual member of a C standardisation group.

In previous threads about this, we found out that he was an
"observing member", a status that anyone can obtain without
special qualifications, and he is not known to have ever attended
a committee meeting.
 
R

Richard Heathfield

U said:

Question to the NG: This is the first time ever I've heard
H.S. was an actual member of a C standardisation group. I've
seen a list of people involved in ANSI C89, but it seems more
difficult to get by a similar list for ISO 9899, both C90 and
C99; can anybody with more insight confirm/debunk this?

Anyone, no matter how clueless, can be an "observing member" of such
groups. If I recall correctly, committee members can only recall
Herbert Schildt attending *ONE* meeting. And I don't know whether I
recall correctly, because I can find no reference to such attendance
anywhere.

"Schildt is an observing member of the committee which just means he
pays his dues. As I understand it nobody has seen him turn up to
observe anything!" - Lawrence Kirby, comp.lang.c, 4 Feb 1998

"[Schildt] sells his books by virtue of membership in the ISO/ANSI C
committee; unfortunately for the readership, most people don't know that
an "observing member" merely has to pay dues, and is not obliged to
attend meetings; none of the ISO folks I know remember seeing him
there, certainly... " - Peter Seebach (ISO C Committee member),
comp.lang.c, 17 October 1996

">"Schildt, an Observing Member of the ANSI C Committee..."
Is this true?? Yes.
Was he there??
No." - Peter Seebach again, comp.lang.c, 22 Nov 1997

"[Schildt has] never turned up to any meeting I attended." - Clive
Feather (ISO C Committee member), comp.std.c, 7 May 1999

"In the many years I've been attending meetings of the C standards
committee(s), I don't recall ever seeing Herb Schildt there. If he
is a "member" at all, it is probably as an "observing member" who
just gets the document mailings." - Doug Gwyn (ISO C Committee member),
comp.std.c, 7 May 1999
 
C

CBFalconer

U said:
Heinrich Pumpernickel wrote:
.... snip ...
its not just some book, its called "C: The Complete Reference",
written by some famous and internationally renowed autor
("...world's leading programming author...", "...is a leading
authority on C and was a member of the ANSI/ISO committee that ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
standardized C...") and it spicifically says.......... ^^^^^^^^^^^^^^

You may also declare main() as void if it does not
return a value.

Question to the NG: This is the first time ever I've heard
H.S. was an actual member of a C standardisation group. I've
seen a list of people involved in ANSI C89, but it seems more
difficult to get by a similar list for ISO 9899, both C90 and
C99; can anybody with more insight confirm/debunk this?

[The only source for above quote I could find is the Amazon
review section of `C: The Complete Reference', quote:

"""
*About the Author*
Herbert Schildt is a leading authority on C and was a
member of the ANSI/ISO committee that standardized C. He
is the author of Teach Yourself C, C++: The complete
Reference, Windows 2000 Programming from the Ground Up,
Java: The Complete Reference, and many other best-sellers.
"""

I believe the story is that Bullschildt paid to be on the
committee, went to one meeting, said little or nothing, listened
even less, and has bragged about it ever since.
 
A

Army1987

Army1987 said:
As far as the C programming language is concerned,
no behavior is defined subsequent to the return of main.

#include <stdlib.h>
#include <stdio.h>
static char *global;
void frob(void) {
puts("Hi, I'm frob().");
puts("If main() hasn't returned yet, global points to local, "
"which is still intact.");
puts("Now, the string pointed to by global is:");
puts(global);
}

int main(void) {
char local[] = "Hello, world!";
global = local;
atexit(frob);
return 0;
}
[1]+ Done gedit foo.c
army1987@army1987-laptop:~$ gcc foo.c -ansi -pedantic -Wall -Wextra -o foo
army1987@army1987-laptop:~$ ./foo
Hi, I'm frob().
If main() hasn't returned yet, global points to local, which is still intact.
Now, the string pointed to by global is:
��llok���r��
army1987@army1987-laptop:~$

Looks like local has already died when frob() is called. Yes, the
behaviour is undefined, but if I comment out the last line of the
body of frob() it correctly displays all the stuff before it.

Try a correct instruction to copy global to local. This needs
strcpy, and a suitable #include.
I guess you missed my point. global is a pointer which points to
an automatic array local to main(). I was showing how a program
can do something even after main() has returned.
 
R

Richard

Mark McIntyre said:
OK, but in that case I suggest you write your best effort at solving
the problem, post it here, and ask for assistance. You could also
google for it.


Then I'm afraid that both your book and your teacher are wrong. The C
Standard REQUIRES main to return an int. Whether you want to return
something or not, you need to at least return 0. Otherwise you are
breaking the rules of C.


To the op - read this :


http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1043284376&answer=1044841143
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top