print "foo" without using ;

R

Rajesh

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 )
 
C

Chris Dollin

Rajesh said:
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.
 
R

Rajesh

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 :(
 
C

Clark S. Cox III

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

Carlos

Rajesh said:
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.
 
K

Kenneth Brody

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

Richard Bos

Clark S. Cox III said:
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
 
A

akarl

Rajesh said:
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
 
M

Mark

akarl said:
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
 
K

Keith Thompson

akarl said:
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){}
}
 
W

Walter Roberson

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 ) {}
 
K

Keith Thompson

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

Walter Roberson

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

akarl

Mark said:
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
 
M

Martin Ambuhl

akarl said:
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.
 
W

Walter Roberson

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

Randy Howard

Rajesh wrote
(in article
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.
 
M

Mark

Randy Howard said:
Rajesh wrote
(in article


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
 
L

Lawrence Kirby

Rajesh wrote
(in article


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
 

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
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top