473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

print "foo" without using ;

I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no.
of ways to achive it.
mine sol. was....

int main()
{
if(printf("foo"))
{ ; }
else
{ ; }

return(0);
}

(don't do tht stupid #define )

Nov 15 '05 #1
37 1963
Rajesh wrote:
I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no.
of ways to achive it.
mine sol. was....

int main()
{
if(printf("foo"))
{ ; }
else
{ ; }

return(0);
}

(don't do tht stupid #define )


I hate to point this out [1], but your solution contains not
zero semicolons, but *three*.

As far as I can tell, any "solution" is at best stylistically
unsatisfactory.

--
Chris "electric hedgehog" Dollin
Stross won one! Farah won one! Langford won TWO!
Nov 15 '05 #2
sorry for tht It was just a too much typo.... used too much to
semicolons

neglect that 'int main()' use 'void main()' and dike out those ';' in
if...else plz.
it works.

once again sorry for tht typo I swear :(

Nov 15 '05 #3
On 2005-08-23 10:18:05 -0400, "Rajesh" <ra**************@yahoo.co.in> said:
I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no.
of ways to achive it.
mine sol. was....
First, why on earth are you abbreviating so much? I don't even know
what you mean by "imp."
Second, is it really too hard to spell the actual subject out in your message?
int main()
{
if(printf("foo"))
{ ; }
else
{ ; }

return(0);
}
Um, you failed. I count three semicolons.
(don't do tht stupid #define )


What stupid #define?

Try:

#include <stdio.h>
int main()
{
if(puts("foo")) {}
}

or

#include <stdio.h>
int main()
{
while(puts("foo"),0) {}
}

or

#include <stdio.h>
int main()
{
switch(puts("foo"))
{
default: {}
}
}
or any of the variations on that theme.

--
Clark S. Cox, III
cl*******@gmail.com

Nov 15 '05 #4


Rajesh wrote:
sorry for tht It was just a too much typo.... used too much to
semicolons

neglect that 'int main()' use 'void main()' and dike out those ';' in
if...else plz.
it works.

once again sorry for tht typo I swear :(


I suspect that your garbage English isn't due to typos.

Nov 15 '05 #5
Clark S. Cox III <cl*******@gmail.com> wrote:
On 2005-08-23 10:18:05 -0400, "Rajesh" <ra**************@yahoo.co.in> said:
I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no.
of ways to achive it.
mine sol. was....
First, why on earth are you abbreviating so much?


Because it's K3wLl!1!
Second, is it really too hard to spell the actual subject out in your message?
In fact, I doubt a Microsoft interviewer used (or, for that matter,
understood) non-Microsoft-style shell variables such as $SUBJECT.
Um, you failed.
From the word Go, never mind the semi-colons.
Try:

#include <stdio.h>
int main()
{
if(puts("foo")) {}
}

or


Or rather, don't try. I wouldn't consider hiring anyone whose first and
preferably only answer was "What on earth for? I prefer to write better
code than that." I wouldn't consider working for a company that asked
such interview questions. I'd rather flip burgers; at least McDonalds is
up front about not requiring more than imbecility from its staff.

Richard
Nov 15 '05 #6
Rajesh wrote:

I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no.
of ways to achive it.
mine sol. was....

int main()
{
if(printf("foo"))
{ ; }
else
{ ; }

return(0);
}

(don't do tht stupid #define )


Well, I see three semicolons in your code.

How about:

10 PRINT "foo"
20 END

Or:

<html><body>foo</body></html>

Or:

(defun foo ()
(format t "foo~%"))

Or:

++++++++++[>++++>++++++++++>+++++++<<<-]
++.<+.+++++++..+++.<++++.<+++[>----<-]>.

++++++++.--------.+++.------.--------.<<+++[>++++<-]>++.<++++++++++.

Oh, you wanted a solution in C. :-)

#include <stdio.h>

int main(int argc,char *argv[])
{
if ( printf("foo\n") )
{
}
}

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

Nov 15 '05 #7
Rajesh wrote:
I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no.
of ways to achive it.
mine sol. was....

int main()
{
if(printf("foo"))
{ ; }
else
{ ; }

return(0);
}

(don't do tht stupid #define )


It's impossible to write any complete program without using semicolon
since the `main' function requires a return statement.

August
Nov 15 '05 #8
"akarl" <fu********@comhem.se> wrote in message
news:GS*********************@newsc.telia.net...
Rajesh wrote:
I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no.
of ways to achive it.
mine sol. was....

int main()
{
if(printf("foo"))
{ ; }
else
{ ; }

return(0);
}

(don't do tht stupid #define )


It's impossible to write any complete program without using semicolon
since the `main' function requires a return statement.


Do you have a copy of the standard (or final draft?)

5.1.2.2.3 - Program termination
.... 9) reaching the } that terminated the main function returns a value of 0
Nov 15 '05 #9
akarl <fu********@comhem.se> writes:
Rajesh wrote:
I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no.
of ways to achive it.
mine sol. was....
int main()
{
if(printf("foo"))
{ ; }
else
{ ; }
return(0);
}
(don't do tht stupid #define )


It's impossible to write any complete program without using semicolon
since the `main' function requires a return statement.


No, it doesn't. In C99, falling off the end of main() is equivalent
to executing "return 0;" (which is a misfeature IMHO). Even in C90,
you can use exit() rather than return.

The original question is stupid, but here's a solution anyway:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
if (puts("hello"),exit(0),0){}
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
akarl <fu********@comhem.se> writes:
Rajesh wrote:
I was informed tht $SUBJECT was asked in M$ interview.
No, it doesn't. In C99, falling off the end of main() is equivalent
to executing "return 0;" (which is a misfeature IMHO). Even in C90,
you can use exit() rather than return. The original question is stupid, but here's a solution anyway: #include <stdio.h>
#include <stdlib.h>
int main(void)
{
if (puts("hello"),exit(0),0){}
}


If one supposes C89, then what would be the exit status if the puts()
fails?

Perhaps something like

if (puts("foo") && (exit(0),0) || (exit(0),0) ) {}

or

if ( puts("foo") ? exit(0),0 : exit(0),0 ) {}
--
"I will speculate that [...] applications [...] could actually see a
performance boost for most users by going dual-core [...] because it
is running the adware and spyware that [...] are otherwise slowing
down the single CPU that user has today" -- Herb Sutter
Nov 15 '05 #11
akarl wrote on 23/08/05 :
It's impossible to write any complete program without using semicolon since
the `main' function requires a return statement.


It's possible in C99.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
Nov 15 '05 #12
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
akarl <fu********@comhem.se> writes:
Rajesh wrote:
I was informed tht $SUBJECT was asked in M$ interview.

No, it doesn't. In C99, falling off the end of main() is equivalent
to executing "return 0;" (which is a misfeature IMHO). Even in C90,
you can use exit() rather than return.

The original question is stupid, but here's a solution anyway:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
if (puts("hello"),exit(0),0){}
}


If one supposes C89, then what would be the exit status if the puts()
fails?


The exit status is 0 regardless of what puts() returns. The comma
operator imposes a sequence point and a left-to-right evaluation order
on its operands, but it doesn't short-circuit; both operands are
always evaluated.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #13
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
if (puts("hello"),exit(0),0){}
The exit status is 0 regardless of what puts() returns. The comma
operator imposes a sequence point and a left-to-right evaluation order
on its operands, but it doesn't short-circuit; both operands are
always evaluated.


Ah, right, I should have twigged onto that. Thanks.
--
The rule of thumb for speed is:

1. If it doesn't work then speed doesn't matter. -- Christian Bau
Nov 15 '05 #14
Mark wrote:
"akarl" <fu********@comhem.se> wrote in message
news:GS*********************@newsc.telia.net...
Rajesh wrote:
I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no.
of ways to achive it.
mine sol. was....

int main()
{
if(printf("foo"))
{ ; }
else
{ ; }

return(0);
}

(don't do tht stupid #define )


It's impossible to write any complete program without using semicolon
since the `main' function requires a return statement.

Do you have a copy of the standard (or final draft?)

5.1.2.2.3 - Program termination
... 9) reaching the } that terminated the main function returns a value of 0


OK, I didn't know that. Seems like quite a few people in this group has
complained when someone has posted a main function lacking a return
statement. Hence my impression of it being mandatory. Without a return
statement we can of course do something like

#include <stdio.h>

int main(void)
{
if (puts("foo")) {}
}
August

Nov 15 '05 #15
akarl wrote:
OK, I didn't know that. Seems like quite a few people in this group has
complained when someone has posted a main function lacking a return
statement. Hence my impression of it being mandatory.


C89 requires a return; C99 doesn't. Almost everyone posting has a C89
compiler; almost no one posting here has a C99 compiler. Doesn't it
make sense to require the return? Consider that there was no good
reason for the C99 standardization committee to allow this one exception
to the rule that functions that return values do so explicitly. The
inexcusable reason is that too many bad programmers had been writing
code in violation of the standard for the last 10 years.
Nov 15 '05 #16
In article <sj*******************@newsb.telia.net>,
akarl <fu********@comhem.se> wrote:
OK, I didn't know that. Seems like quite a few people in this group has
complained when someone has posted a main function lacking a return
statement. Hence my impression of it being mandatory.


C89 and C99 differ on this point. C99 -defines- falling out of main
as an exit with 0 status. C89 says that if you do that then the resulting
exit status is undefined. An undefined exit status might not matter
much of the time, but you don't want to get into the bad habit of
leaving it up to chance.
--
Feep if you love VT-52's.
Nov 15 '05 #17
Rajesh wrote
(in article
<11**********************@g43g2000cwa.googlegroups .com>):
neglect that 'int main()' use 'void main()'


I wish more candidates would try "void main()" right away, it
would shorten those interviews and leave more time for the
better candidates.
--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #18
"Randy Howard" <ra*********@FOOverizonBAR.net> wrote in message
news:00*****************************@news.verizon. net...
Rajesh wrote
(in article
<11**********************@g43g2000cwa.googlegroups .com>):
neglect that 'int main()' use 'void main()'


I wish more candidates would try "void main()" right away, it
would shorten those interviews and leave more time for the
better candidates.


You assume that the people conducting the interviews
know better? I'd wager that many would view void main()
as being perfectly acceptable.

The problem stems from the fact that many text books
teach people to program using void main(); in their
examples. Unless one is lucky enough to stumble
across one of the better books (or a forum such as
comp.lang.c) which points out the (portability) issue
they'll never know that void main(); is inappropriate.

Mark
Nov 15 '05 #19
On Tue, 23 Aug 2005 21:32:17 +0000, Randy Howard wrote:
Rajesh wrote
(in article
<11**********************@g43g2000cwa.googlegroups .com>):
neglect that 'int main()' use 'void main()'


I wish more candidates would try "void main()" right away, it
would shorten those interviews and leave more time for the
better candidates.


Don't forget this was a "M$ interview". The normal rules of C don't apply,
which makes this question off topic. :-)

Lawrence

Nov 15 '05 #20
On Tue, 23 Aug 2005 20:16:37 +0000, Martin Ambuhl wrote:
akarl wrote:
OK, I didn't know that. Seems like quite a few people in this group has
complained when someone has posted a main function lacking a return
statement. Hence my impression of it being mandatory.


C89 requires a return; C99 doesn't. Almost everyone posting has a C89
compiler; almost no one posting here has a C99 compiler. Doesn't it
make sense to require the return? Consider that there was no good
reason for the C99 standardization committee to allow this one exception
to the rule that functions that return values do so explicitly. The
inexcusable reason is that too many bad programmers had been writing
code in violation of the standard for the last 10 years.


C89 doesn't require a return. If you fall off the end of main() you get an
undefined termination status but not an invalid program. If the
program doesn't fall off the end of main() there's no problem at all.

However an undefined termination status is undesirable in normal
circumstances so falling off the end of main() is considered a bad thing,
hence the initial point above.

Lawrence

Nov 15 '05 #21
Martin Ambuhl wrote:

akarl wrote:
OK, I didn't know that. Seems like quite a few people in this group has
complained when someone has posted a main function lacking a return
statement. Hence my impression of it being mandatory.


C89 requires a return; C99 doesn't. Almost everyone posting has a C89
compiler; almost no one posting here has a C99 compiler. Doesn't it
make sense to require the return? Consider that there was no good
reason for the C99 standardization committee to allow this one exception
to the rule that functions that return values do so explicitly. The
inexcusable reason is that too many bad programmers had been writing
code in violation of the standard for the last 10 years.


Hmm... Using MSVC (hardly a "standard", I know)...

int main()
{
foo();
}
int foo()
{
}

This complains that foo() doesn't have a return, but doesn't complain
about main() not having a return. (Even with warnings all the way up.)
So, they appear to allow C99's exception. However, looking at the
generated assembly, they still follow the older "return from main is
the value of the last statement". (Basically, in MSVC's case, it means
that EAX isn't assigned any specific value, and so main will return
whatever foo returned.)

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

Nov 15 '05 #22
Mark wrote
(in article <MW*****************@newshog.newsread.com>):
"Randy Howard" <ra*********@FOOverizonBAR.net> wrote in message
news:00*****************************@news.verizon. net...
Rajesh wrote
(in article
<11**********************@g43g2000cwa.googlegroups .com>):
neglect that 'int main()' use 'void main()'
I wish more candidates would try "void main()" right away, it
would shorten those interviews and leave more time for the
better candidates.


You assume that the people conducting the interviews
know better? I'd wager that many would view void main()
as being perfectly acceptable.


Since I was speaking about myself, I know that the interviewer
knows better. :-)
The problem stems from the fact that many text books
teach people to program using void main(); in their
examples.
Most famously Schildt I suspect. Since for some reason authors
that commit this mistake to print are extremely reluctant to
correct it in subsequent printings or even errata (I know,
because I have tried on several occasions, and been blown off or
insulted for my troubles, despite all attempts at making the
case politely).
Unless one is lucky enough to stumble
across one of the better books (or a forum such as
comp.lang.c) which points out the (portability) issue
they'll never know that void main(); is inappropriate.


Unless they bother to actually check a standard document.

I do wish gcc would spit out a warning on void main() except for
the embedded variants.
--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #23
Randy Howard <ra*********@FOOverizonBAR.net> writes:
[...]
I do wish gcc would spit out a warning on void main() except for
the embedded variants.


It does. I just tried versions 2.8.1, 2.95.2, 3.0.4, 3.4.4, and
4.0.0; they all print

void-main.c:2: warning: return type of `main' is not `int'

for the following:

void main(void)
{
}

even without any special options.

Unless you're saying you *don't* want the warning for the embedded
variants?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #24
Randy Howard wrote:
Rajesh wrote
(in article
<11**********************@g43g2000cwa.googlegroups .com>):
neglect that 'int main()' use 'void main()'


I wish more candidates would try "void main()" right away, it
would shorten those interviews and leave more time for the
better candidates.


Mostly off-topic, but perhaps surprising is the fact that Microsoft
actually accepts void as a valid return type.

http://msdn.microsoft.com./library/d...n_function.asp

--
Sean
Nov 15 '05 #25
Rajesh wrote:
I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no.
of ways to achive it.
mine sol. was....


Maybe I'm wrong, but I think everybody here is taking this too far. I
believe the intent of the interview question applied only to the line
printing "foo", which would make a return statement at the end a
perfectly legal solution.

#include <stdio.h>

int main(void)
{
if (printf("foo\n"))
{
}

return 0;
}

Agreed, it's a stupid question. But knowing the answer does
--somewhat-- prove an open-minded solution to a problem that tests ones
knowledge of C.

I already posted this link in another reply; but, it should be noted
that Microsoft is one of the few implementors that *has* defined a void
return type for function main().

http://msdn.microsoft.com./library/d...n_function.asp

--
Sean
Nov 15 '05 #26
Fao, Sean wrote:

Mostly off-topic, but perhaps surprising is the fact that Microsoft
actually accepts void as a valid return type.

http://msdn.microsoft.com./library/d...n_function.asp


Sometimes I wonder if they're deliberately trying to break things.

-- Denis
Nov 15 '05 #27
Fao, Sean wrote:
Randy Howard wrote:
Rajesh wrote
(in article <11**********************@g43g2000cwa.googlegroups .com>):
neglect that 'int main()' use 'void main()'


I wish more candidates would try "void main()" right away, it would
shorten those interviews and leave more time for the better candidates.


Mostly off-topic, but perhaps surprising is the fact that Microsoft
actually accepts void as a valid return type.

http://msdn.microsoft.com./library/d...n_function.asp


Not at all surprising around here since we have discussed the fact that
MS often declares main as returning void in its examples.

The standard does not prohibit MS from doing this, it is just that it is
not portable since not all systems define it.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #28
"Fao, Sean" <en**********@yahoo.comI-WANT-NO-SPAM> writes:
[...]
Mostly off-topic, but perhaps surprising is the fact that Microsoft
actually accepts void as a valid return type.

http://msdn.microsoft.com./library/d...n_function.asp


That web page refers to C++, not C.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #29
Fao, Sean wrote:
Rajesh wrote:
I was informed tht $SUBJECT was asked in M$ interview. Real imp.
is no. of ways to achive it. mine sol. was....


Maybe I'm wrong, but I think everybody here is taking this too
far. I believe the intent of the interview question applied only
to the line printing "foo", which would make a return statement at
the end a perfectly legal solution.

#include <stdio.h>

int main(void)
{
if (printf("foo\n"))
{
}

return 0;
}


Unlikely, eg:

#include <stdio.h>
int main()
{
/* here is the line that prints foo! */
puts("foo")

/* there it was */

; return 0;
}

Of course, as has been pointed out on this thread, main does not
need a return statement.

Nov 15 '05 #30
Denis Kasak <de*********@gmail.com> wrote:
Fao, Sean wrote:

Mostly off-topic, but perhaps surprising is the fact that Microsoft
actually accepts void as a valid return type.

http://msdn.microsoft.com./library/d...n_function.asp


Sometimes I wonder if they're deliberately trying to break things.


....and then you wake up and realise that they've been known to do so,
and deliberately, for decades?

Richard
Nov 15 '05 #31
Richard Bos wrote:

....and then you wake up and realise that they've been known to do so,
and deliberately, for decades?


I guess I'm still living in my little fantasy world where Microsoft
doesn't do so much damage to everything.

-- Denis
Nov 15 '05 #32
On Fri, 26 Aug 2005 07:46:42 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Denis Kasak <de*********@gmail.com> wrote:
Fao, Sean wrote:
>
> Mostly off-topic, but perhaps surprising is the fact that Microsoft
> actually accepts void as a valid return type.
>
> http://msdn.microsoft.com./library/d...n_function.asp


Sometimes I wonder if they're deliberately trying to break things.


...and then you wake up and realise that they've been known to do so,
and deliberately, for decades?


Actually, that entry in the MSDN has been updated in the last year or
so. For a very long time, it said that main must return an int, and
immediately followed it by an example showing main returning void. Go
figure...

I also find the bit about exit() remarkable.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #33
On Thu, 25 Aug 2005 18:53:22 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
"Fao, Sean" <en**********@yahoo.comI-WANT-NO-SPAM> writes:
[...]
Mostly off-topic, but perhaps surprising is the fact that Microsoft
actually accepts void as a valid return type.

http://msdn.microsoft.com./library/d...n_function.asp


That web page refers to C++, not C.


Which is even more ridiculous of course.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #34
Fao, Sean wrote:
Mostly off-topic, but perhaps surprising is the fact that Microsoft
actually accepts void as a valid return type.

http://msdn.microsoft.com./library/d...n_function.asp


Not surprising here. The main reason that an OS (or other calling
environment) wouldn't accept a void function, rather than an function
returning int is that the calling convention is incompatible. Since C
evolved from K&R, which didn't have void types, and therefore used
default int type functions. When ANSI C was being developed and a void
type added, implementors wisely did not make calling void functions
incompatible with calling an int function. Of course, such calling
compatibility isn't guaranteed, but happens most of the time. What
implementations are you familiar with in which the calling convention
for a void function and an int function are different? I am not aware
of any.

Thad

Nov 15 '05 #35

In article <43***********************@auth.newsreader.octanew s.com>, Thad Smith <Th*******@acm.org> writes:
Fao, Sean wrote:
Mostly off-topic, but perhaps surprising is the fact that Microsoft
actually accepts void as a valid return type.
Not surprising here. The main reason that an OS (or other calling
environment) wouldn't accept a void function, rather than an function
returning int is that the calling convention is incompatible.


It's not a matter for the OS, but one for the implementation. An
implementation which runs on top of an OS might disallow "void main"
as an alternative, non-standard form because of some issue with that
OS, but there are certainly other possibilities. For example, an
implementation might disallow it because its authors wish to enforce
explicitly returning a valid status.

I'm curious to know how you determined that "calling convention"
(which is not covered by the standard) is the "main reason" for
disallowing alternative forms of main.
When ANSI C was being developed and a void
type added, implementors wisely did not make calling void functions
incompatible with calling an int function.
In what sense? The standard certainly makes calling a function
incorrectly - ie, with an incorrect prototype in scope, or without
a prototype in scope if the function is not declared in a manner
compatible with a non-prototyped function - an error, which sounds
like "incompatible" to me.
Of course, such calling compatibility isn't guaranteed,
If the standard doesn't guarantee compatibility, then it makes it
incompatible. There is no middle. "Happens to work" is a possible
result of undefined behavior; it doesn't demonstrate anything about
either the intentions or requirements of the standard.
What
implementations are you familiar with in which the calling convention
for a void function and an int function are different?
EPM C for the AS/400, for one.
I am not aware of any.


Anecdotal evidence, as always, demonstrates nothing about the C
language.

--
Michael Wojcik mi************@microfocus.com

When [Columbus] landed on America it was more like an evasion than a
discovery. -- Matt Walsh
Nov 15 '05 #36
Thad Smith wrote:
Fao, Sean wrote:
Mostly off-topic, but perhaps surprising is the fact that Microsoft
actually accepts void as a valid return type.

http://msdn.microsoft.com./library/d...n_function.asp


Not surprising here. The main reason that an OS (or other calling
environment) wouldn't accept a void function, rather than an function
returning int is that the calling convention is incompatible. Since C
evolved from K&R, which didn't have void types, and therefore used
default int type functions. When ANSI C was being developed and a void
type added, implementors wisely did not make calling void functions
incompatible with calling an int function. Of course, such calling
compatibility isn't guaranteed, but happens most of the time. What
implementations are you familiar with in which the calling convention
for a void function and an int function are different? I am not aware
of any.

Thad

Utter nonsense. How can you possibly offer such trash in the group where
you know the masters live.

1. There is no term 'calling environment' in common parlance.
2. The term 'calling convention' is commonly used but you obviously
don't know what it means (look it up).
3. As of C89 we are allowed to declare and define generic functions of
type void. Nothing to do with calling convention.

The function main() is NOT a generic. It is the real name of your C
program. It is called by the command shell and has a 'value' of type
int. Not void, int! This was decided among Ken Thompson and Dennis
Ritchie circa 1972, three years before Bill, 19 years old in 1975
started Microsoft.

K&R (1978), C89 and C99 all define main() with type int.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #37
Joe Wright <jw*****@comcast.net> writes:
Thad Smith wrote:
Fao, Sean wrote:
Mostly off-topic, but perhaps surprising is the fact that Microsoft
actually accepts void as a valid return type.

http://msdn.microsoft.com./library/d...n_function.asp Not surprising here. The main reason that an OS (or other calling
environment) wouldn't accept a void function, rather than an
function returning int is that the calling convention is
incompatible. Since C evolved from K&R, which didn't have void
types, and therefore used default int type functions. When ANSI C
was being developed and a void type added, implementors wisely did
not make calling void functions incompatible with calling an int
function. Of course, such calling compatibility isn't guaranteed,
but happens most of the time. What implementations are you familiar
with in which the calling convention for a void function and an int
function are different? I am not aware of any.
Thad

Utter nonsense. How can you possibly offer such trash in the group
where you know the masters live.


Actually, I think he's basically correct.
1. There is no term 'calling environment' in common parlance.
The term occurs in the standard, though in a slightly different
context, and the meaning seems clear enough.
2. The term 'calling convention' is commonly used but you obviously
don't know what it means (look it up).
3. As of C89 we are allowed to declare and define generic functions of
type void. Nothing to do with calling convention.

The function main() is NOT a generic. It is the real name of your C
program. It is called by the command shell and has a 'value' of type
int. Not void, int! This was decided among Ken Thompson and Dennis
Ritchie circa 1972, three years before Bill, 19 years old in 1975
started Microsoft.

K&R (1978), C89 and C99 all define main() with type int.


The reasons for making void functions and int functions de facto
compatible aren't related to main(), since main() has always returned
int. It has to do with other functions.

For example, in pre-ANSI C, a function that was not intended to return
a value might be have been written as:

do_something()
{
...
}

The function implicitly returns int, but it doesn't execute a return
statement and the callers don't attempt to use the result.

In ANSI C, it might be written as:

void do_something(void)
{
...
}

In the transition to ANSI C, many implementers presumably used
compatible calling sequences for these two cases to avoid breaking
existing code, or to allow pre-ANSI code to call ANSI code and vice
versa, or to allow C code to call assembly code, etc.

The fact that such implementations might make void main() compatible
with int main() is just a side effect (an unfortunate one, IMHO).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #38

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Carroll | last post by:
I'm at my wits end with this silly thing. I'm new to python, so please excuse my impending ignorance ;). I am trying to execute a command, and get the output back into a string variable. Here's...
3
by: Thomas Guettler | last post by:
Hi, Python 2.3.3 (#1, Feb 5 2005, 16:22:10) on linux2 >>> assert 0, "foo" Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError: foo >>> assert(0, "foo") >>>
9
by: David D. | last post by:
Does the file extension matter when including a JavaScript file in an HTML page? Normally, one would include a JavaScript file in an HTML page using <script src="foo.JS" type="text/javascript">...
5
by: vilhelm.sjoberg | last post by:
Hello, I am a resonably confident C programmer, but not very sure about the dark corners of C++. Recently, I had G++ give me a strange error. The program in question is in essence: struct...
1
by: dhtmlkitchen | last post by:
Why does "foo" instanceof String return false? It just seems counterintuitive to me. I mean, sure, I can always check the constructor: var fooVar = "foo"; var isString =...
30
by: kj | last post by:
My book (Flanagan's JavaScript: The Definitive Guide, 5th ed.) implies on page 111 that the following two constructs are equivalent: ( x.constructor == Foo ) and ( x instanceof Foo ) The...
1
by: TP | last post by:
Hi everybody, All my problem is in the title. If I try: $ python -c 'print "foo",' It does not change anything, surely because the line return is added by "python -c".
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.