Void Main?

W

Wang Yip

Hello I read Main shall not return Void but I always use and always
work.... who is right
 
E

Eric Sosman

Hello I read Main shall not return Void but I always use and always
work.... who is right

The function named Main() can return anything you like.
Anything, or nothing. No restrictions: Do what you will,
even to the extent of returning a value of type Void (which
you can #define or typedef as you please).

In a hosted implementation, the function named main()
and having external linkage must be defined as returning an
int value, and must actually return one if it returns at all.
Under C99 rules, "falling off the end" of this function returns
the int value zero; under earlier versions of the Standard the
return had to be explicit.

... and this topic has been BEATEN TO DEATH so many times
that I cannot imagine anyone wanting to go through it yet again.
 
T

Tim Prince

superpollo said:
Wang Yip ha scritto:

maybe some compilers are more forgiving than others ?
Many compilers have options to turn off standards checking (or skip it
by default), which may be OK if you don't intend your run-time
environment to see the success/failure return value.
 
R

Rich Webb

The function named Main() can return anything you like.
Anything, or nothing. No restrictions: Do what you will,
even to the extent of returning a value of type Void (which
you can #define or typedef as you please).

In a hosted implementation, the function named main()
and having external linkage must be defined as returning an
int value, and must actually return one if it returns at all.
Under C99 rules, "falling off the end" of this function returns
the int value zero; under earlier versions of the Standard the
return had to be explicit.

... and this topic has been BEATEN TO DEATH so many times
that I cannot imagine anyone wanting to go through it yet again.

'Cause it's a quiet day otherwise? ;-)
 
K

Keith Thompson

Eric Sosman said:
The function named Main() can return anything you like.
Anything, or nothing. No restrictions: Do what you will,
even to the extent of returning a value of type Void (which
you can #define or typedef as you please).

In case you misseed it, Eric's point is that C is case-sensitive;
"Main" and "main" are two distinct and unrelated identifiers.
In a hosted implementation, the function named main()
and having external linkage must be defined as returning an
int value, and must actually return one if it returns at all.
Under C99 rules, "falling off the end" of this function returns
the int value zero; under earlier versions of the Standard the
return had to be explicit.

And if you incorrectly declare main as "void main(void)", your
program's behavior is undefined. It's an error that your compiler
isn't required to diagnose. So if your compiler quietly accepts it
and your program appears to work, that doesn't mean either that you
got it right or that your compiler got it wrong. It's entirely *your*
responsibility to get this right.

The valid declarations for main are:
int main(void)
int main(int argc, char *argv[])
The latter is equivalent to
int main(int argc, char **argv)
 
K

Kaz Kylheku

Hello I read Main shall not return Void but I always use and always
work.... who is right

Of what value is your ``always''?

How different are your uses from each other?

If you use ``viod main'' in five hundred different programs, and it
always works, but they all use the same compiler, run-time library
and operating system, don't you think that these 500 programs
are really just one test case? Don't you think that these 500 programs
demonstrate only a /single/ data point: that void main works with
that compiler and run-time environment?

It's a big jump from a single data point to ``always''.

Consider this: C has a static type system. Functions have a type.
The function int X(int) is not compatible with void Y(int) .

If you were writing a device driver at an embedded company, and one
of the functions is declared as a pointer:

int (*transmit)(netdev *dev, netpacket *);

would you implement it like this:

void my_transmit(netdev *dev, netpacket *);

?

Your compiler would tell you that the address of my_transmit
is not assignment-compatible to the transmit function pointer:

static netdev dev = { ..., my_transmit, ...}; /* error */

Suppose it's only a warning, and it compiles anyway. When the OS calls
the driver's my_transmit, what do you think happens when the function
returns? The caller expects a return value, but the function is void.

Can you point the newsgroup to the chapter of some document
which defines what happens in that situation?

What makes you think you can just pick a random function type out of the
air and use that for your main function?
 
W

Wang Yip

Thanks for reply. I tried search for Google groups before ask here but
didn't find any answers.......
 
J

Johannes Schaub (litb)

Keith said:
In case you misseed it, Eric's point is that C is case-sensitive;
"Main" and "main" are two distinct and unrelated identifiers.


And if you incorrectly declare main as "void main(void)", your
program's behavior is undefined. It's an error that your compiler
isn't required to diagnose. So if your compiler quietly accepts it
and your program appears to work, that doesn't mean either that you
got it right or that your compiler got it wrong. It's entirely *your*
responsibility to get this right.

I was in the impression that the C99 Standard allows implementations to
provide additional return types. If an implementation supports "void", then
behavior is not undefined. This is what i infer out of the "It shall be
defined with return type of int ... or in some other implementation-defined
manner."

If, however, the implementation does not allow it, behavior is undefined. Is
my interpretation wrong?
 
K

Keith Thompson

Johannes Schaub (litb) said:
Keith Thompson wrote: [...]
And if you incorrectly declare main as "void main(void)", your
program's behavior is undefined. It's an error that your compiler
isn't required to diagnose. So if your compiler quietly accepts it
and your program appears to work, that doesn't mean either that you
got it right or that your compiler got it wrong. It's entirely *your*
responsibility to get this right.

I was in the impression that the C99 Standard allows implementations to
provide additional return types. If an implementation supports "void", then
behavior is not undefined. This is what i infer out of the "It shall be
defined with return type of int ... or in some other implementation-defined
manner."

If, however, the implementation does not allow it, behavior is undefined. Is
my interpretation wrong?

You're right. I left that out in the vain hope of avoiding yet
another lengthy discussion.

Yes, an implementation may permit other declarations for main,
including "void main(void)". If a given implementation permits that
declaration (and documents it), then such a program's behavior is
well defined *for that implementation*.

There is no good reason for an implementation to permit "void
main(void)", and no good reason for a programmer to take advantage
of it. "int main(void)" works everywhere.

One quibble: on freestanding implementations (roughly speaking,
embedded systems), "void main(void)" might be perfectly sensible.
But most beginners don't use such implementations.
 
W

Wang Yip

Hi What is the Void in parenthesis. I am familiar
Main() {...}
and
Void Main() {...}
but what is the
Void Main(Void)
mean. How to pass one Void argument. Do you intend Void Main(Void *)....

I find C very confused........

Johannes Schaub (litb) said:
Keith Thompson wrote:
[...]
And if you incorrectly declare main as "void main(void)", your
program's behavior is undefined. It's an error that your compiler
isn't required to diagnose. So if your compiler quietly accepts it
and your program appears to work, that doesn't mean either that you
got it right or that your compiler got it wrong. It's entirely *your*
responsibility to get this right.

I was in the impression that the C99 Standard allows implementations to
provide additional return types. If an implementation supports "void", then
behavior is not undefined. This is what i infer out of the "It shall be
defined with return type of int ... or in some other implementation-defined
manner."

If, however, the implementation does not allow it, behavior is undefined. Is
my interpretation wrong?


You're right. I left that out in the vain hope of avoiding yet
another lengthy discussion.

Yes, an implementation may permit other declarations for main,
including "void main(void)". If a given implementation permits that
declaration (and documents it), then such a program's behavior is
well defined *for that implementation*.

There is no good reason for an implementation to permit "void
main(void)", and no good reason for a programmer to take advantage
of it. "int main(void)" works everywhere.

One quibble: on freestanding implementations (roughly speaking,
embedded systems), "void main(void)" might be perfectly sensible.
But most beginners don't use such implementations.
 
K

Keith Thompson

Wang Yip said:
Hi What is the Void in parenthesis. I am familiar
Main() {...}
and
Void Main() {...}
but what is the
Void Main(Void)
mean. How to pass one Void argument. Do you intend Void Main(Void *)....

I find C very confused........
[...]

Please don't top-post. See:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

C is case-sensitive. "Main" and "main" are two distinct identifiers,
as are "Void" and "void".

In a function declaration, the "(void)" syntax means that the
function has no parameters. It would have made more sense to use
"()" for this, but for historical reasons this special-case syntax
was necessary (empty parentheses already had a different meaning).

This is a very elementary question. We're here to help (well,
most of us are), but you're not going to have much luck learning
the language by asking one question at a time. I think you'll be
more successful if you use a good book. I recommend Kernighan &
Ritchie's "The C Programming Language", 2nd edition, affectionately
known as K&R2. There are also some good online tutorials (and a
whole lot of really bad ones).

See questions 18.9 and 18.10 of the comp.lang.c FAQ,
<http://www.c-faq.com/>, and follow the links. (The FAQ is an
excellent resource, but not suitable for learning the language
from scratch.)
 
W

Wang Yip

Hi yet I not understand... How to pass a Void argument.

In the code I use small letters but I find clearer to write with big
letters Void to show is C type not word. I can not afford book... pls
send a rapidshare link for "The C Programming Language" thanks......


Wang Yip said:
Hi What is the Void in parenthesis. I am familiar
Main() {...}
and
Void Main() {...}
but what is the
Void Main(Void)
mean. How to pass one Void argument. Do you intend Void Main(Void *)....

I find C very confused........

[...]

Please don't top-post. See:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

C is case-sensitive. "Main" and "main" are two distinct identifiers,
as are "Void" and "void".

In a function declaration, the "(void)" syntax means that the
function has no parameters. It would have made more sense to use
"()" for this, but for historical reasons this special-case syntax
was necessary (empty parentheses already had a different meaning).

This is a very elementary question. We're here to help (well,
most of us are), but you're not going to have much luck learning
the language by asking one question at a time. I think you'll be
more successful if you use a good book. I recommend Kernighan &
Ritchie's "The C Programming Language", 2nd edition, affectionately
known as K&R2. There are also some good online tutorials (and a
whole lot of really bad ones).

See questions 18.9 and 18.10 of the comp.lang.c FAQ,
<http://www.c-faq.com/>, and follow the links. (The FAQ is an
excellent resource, but not suitable for learning the language
from scratch.)
 
K

Keith Thompson

Wang Yip said:
Hi yet I not understand... How to pass a Void argument.

There is no such thing as a void argument. As I explained, in
"int main(void)" the "(void)" is a special-case syntax that means
that the function takes no arguments.
In the code I use small letters but I find clearer to write with big
letters Void to show is C type not word. I can not afford book... pls
send a rapidshare link for "The C Programming Language" thanks......
[...]

Again, please don't top-post. Your response belongs *below* any
quoted text, not above it. Please read the following links before
posting again:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

Capitalizing words such as "void" and "main" that are inherently
lowercase is not helpful, and will only cause confusion. If you
want to make it clear that you're referring to a C keyword or
identifier rather than an English word, find some other way to
do it; quotation marks are usually a reasonable way to do this.
In this context, though, even that probably isn't necessary; it's
clear enough from context what void and main mean.

I already cited questions 18.9 and 18.10 of the comp.lang.c FAQ,
<http://www.c-faq.com/>. If you had read them, you would have
found several online tutorials. I don't know what "rapidshare" is,
but as far as I know there are no *legitimate* copies of K&R2 other
than printed ones, and I won't help you obtain an illegitimate copy.
 
W

Wang Yip

Wang Yip said:
Hi yet I not understand... How to pass a Void argument.


There is no such thing as a void argument. As I explained, in
"int main(void)" the "(void)" is a special-case syntax that means
that the function takes no arguments.

In the code I use small letters but I find clearer to write with big
letters Void to show is C type not word. I can not afford book... pls
send a rapidshare link for "The C Programming Language" thanks......

[...]

Again, please don't top-post. Your response belongs *below* any
quoted text, not above it. Please read the following links before
posting again:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

Capitalizing words such as "void" and "main" that are inherently
lowercase is not helpful, and will only cause confusion. If you
want to make it clear that you're referring to a C keyword or
identifier rather than an English word, find some other way to
do it; quotation marks are usually a reasonable way to do this.
In this context, though, even that probably isn't necessary; it's
clear enough from context what void and main mean.

I already cited questions 18.9 and 18.10 of the comp.lang.c FAQ,
<http://www.c-faq.com/>. If you had read them, you would have
found several online tutorials. I don't know what "rapidshare" is,
but as far as I know there are no *legitimate* copies of K&R2 other
than printed ones, and I won't help you obtain an illegitimate copy.
Hi Thanks to your great help.... I understand now but when I try below
program there is error.

#define V (void)x

f(void)
{
printf("calling f.........................\n");
}

main()
{
int x;
f(V);
}

I also try with int main(void)..... But then if I give a command line
parameter there is no error but I understand you say (void) is NO arguments.

Rapidshare is free download site... look for it, many good warez.
 
S

Seebs

I was in the impression that the C99 Standard allows implementations to
provide additional return types.

It does, so does C89.
If an implementation supports "void", then
behavior is not undefined. This is what i infer out of the "It shall be
defined with return type of int ... or in some other implementation-defined
manner."
If, however, the implementation does not allow it, behavior is undefined. Is
my interpretation wrong?

I don't think so. So I think that, if the documentation explicitly says
that an alternative declaration is acceptable, it's no longer undefined,
but it's reliant on implementation-defined behavior. There are other examples
of such things:

unsigned char *x;
long l = 3;
x = malloc(sizeof(int));
memcpy(x, &l, sizeof(long));

Obviously, this code is not portable, but whether or not it invokes undefined
behavior is implementation-defined. On a platform where sizeof(long) is the
same as sizeof(int), it does nothing undefined, it's merely stupid.

-s
 
S

Seebs

Hi What is the Void in parenthesis.

There isn't one.

Please stop capitalizing bits of code, that is incorrect. If you are trying
to use the capitalization to call attention to a word, or separate it from
the surrounding text, try:
What is the "void" in parentheses?

(Note also that "parenthesis" is singular, and that questions usually
take question marks.)
I find C very confused........

C isn't all that confused, but you are.

-s
p.s.: In a function declaration or definition, a parameter list consisting
only of the word "void" is an explicit assertion that the function takes
no arguments; in a definition, this is the same as an empty list, but in
a declaration, an empty list might indicate a function about whose calling
convention nothing is known.
 
S

Seebs

Hi yet I not understand... How to pass a Void argument.

You don't. That's used when there's no argument.
In the code I use small letters but I find clearer to write with big
letters Void to show is C type not word.

Learn about quotes. (").
I can not afford book... pls
send a rapidshare link for "The C Programming Language" thanks......

Yeah, we'll totally help you steal stuff. Not.

If you can't afford a book, there's some online tutorials, but they aren't
of great quality. My suggestion would be to save up for a book; my
recommendation is usually Kim King's _C Programming: A Modern Approach_.
The second edition is in print last I checked, and quite good -- it is the
first C book I've ever seen that I'd actually recommend instead of K&R.

-s
 
S

Seebs

Hi Thanks to your great help.... I understand now but when I try below
program there is error.

Then you don't understand now.
#define V (void)x
Wrong.

f(void)
{
printf("calling f.........................\n");
}
main()
{
int x;
f(V);
}

The correct call would be:

f();
I also try with int main(void)..... But then if I give a command line
parameter there is no error but I understand you say (void) is NO arguments.

Right. (void) means no function arguments, it doesn't say anything about
what the calling enviroment might have done.
Rapidshare is free download site... look for it, many good warez.

I'd like to walk you through this slowly.

You are writing to programmers. Do you know what programmers do for a
living, in general? They write programs. Many of these programs are
sold. If the program does not sell, there is no job for the programmer.
Some of us write books. If the book does not sell, there is no job for
the writer. (I'm glossing over internal-use-only code, of which there is
a fair bit, because it's a bit of a special case.)

In short, you're coming to a group of people most of whom make money selling
books or programs, and telling them that you are a big fan of downloading
things without paying the creators for them. Now, can you imagine any way
in which this could make people less likely to help you? I sure can.

-s
 
K

Kenny McCormack

Keith Thompson said:
C is case-sensitive. "Main" and "main" are two distinct identifiers,
as are "Void" and "void".

Strictly speaking, void is not an identifier. Main, main, and Void
are, though.

Isn't infinite reducibility fun?
 
W

Wang Yip

Then you don't understand now.




The correct call would be:

f();




Right. (void) means no function arguments, it doesn't say anything about
what the calling enviroment might have done.




I'd like to walk you through this slowly.

You are writing to programmers. Do you know what programmers do for a
living, in general? They write programs. Many of these programs are
sold. If the program does not sell, there is no job for the programmer.
Some of us write books. If the book does not sell, there is no job for
the writer. (I'm glossing over internal-use-only code, of which there is
a fair bit, because it's a bit of a special case.)

In short, you're coming to a group of people most of whom make money selling
books or programs, and telling them that you are a big fan of downloading
things without paying the creators for them. Now, can you imagine any way
in which this could make people less likely to help you? I sure can.

-s For call f(); I will just write
f(){...}
not
f(void){...}

What is difference with void. Both no arguments.

What is calling environment???? Is not what I learn.

Writers of book are very rich.. I am very poor.. I think they are happy
to share books to developing countries......
 

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,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top